Tomcat连接池出错:java.sql.SQLException: Cannot load JDBC driver class 'null'---
可以成功初始化Context,但是getConnection出错
java.sql.SQLException: Cannot load JDBC driver class 'null'
我的代码如下:
package SunShineSCM;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.*;
import java.sql.*;
public class Users{
String foo = "Not Connected";
int bar = -1;
public void init() {
foo += " 1";
try{
foo += " 2";
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
foo += " 3-----------------";
if(ctx == null )
{
foo += " 4------------ ";
throw new Exception(" the Context initialize is failure Boom - No Context");
}
// ds = (DataSource)envCtx.lookup("jdbc/EmployeeDB");
// con = ds.getConnection();
// DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/JNDISunShineSCM");
DataSource ds = (DataSource)ctx.lookup("jdbc/JNDISunShineSCM");
foo += " Begin Get DataSource ";
if (ds != null) {
foo = " the Context initialize is success ";
foo = "Begin get connection ";
Connection conn = ds.getConnection();
foo = "End get connection";
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery( "select * from users");
if(rst.next()) {
foo=rst.getString(1);
bar=rst.getInt(2);
}
conn.close();
foo =" Get DataSource Sucessed ";
}
}
}catch(Exception e) {
foo += " catch -----------------error = "+ e.toString() +"-----" + e.getMessage() ;
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public int getBar() { return bar;}
}
请大家帮忙