日期:2014-05-18  浏览次数:20738 次

J2EE连接数据库报错空指针了。
这段是数据库连接获取数据的代码:
package kignbi.dong;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {

Connection connect = null;
ResultSet rs = null;
public DB(DataSource dataSource) {

try {
connect = dataSource.getConnection();
}
catch(SQLException e) {
e.printStackTrace();
}
}

public ResultSet OpenSql(String sql) {
try {
Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);

catch(SQLException ex) { 
ex.printStackTrace();
}
return rs;
}


public int ExecSql(String sql) {
int result = 0;
try {
Statement stmt = connect.createStatement();
result = stmt.executeUpdate(sql);

catch(SQLException ex) { 
System.err.println(ex.getMessage());

}
return result;
}

public void close(){
if(connect!=null){
try{
connect.close();
connect = null;
}catch(SQLException ex) { 
System.err.println(ex.getMessage());
}
}
}
}

还有浏览器报错的提示:
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

javax.servlet.ServletExceptionjava.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
kignbi.dong.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:46)


root cause 

java.lang.NullPointerException
kignbi.dong.DB.OpenSql(DB.java:25)
kignbi.dong.action.UserLoginAction.execute(UserLoginAction.java:69)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpSe