连接时JDBC连接NullPointerException

时间:2015-04-16 12:14:05

标签: java jdbc nullpointerexception

我创建了一个简单的java类,它连接到数据库并打印出一些数据。 当我调试时,我的类停在我尝试建立连接的位置并抛出异常NullPointerException。

这是我的代码

public class test_con {
    public static void main(String[] args) throws SQLException {
        OdbcConnection dbcon = new OdbcConnection();
        Connection con = dbcon.getConnection();
        String query = "SELECT Client Code FROM Clients";
        Statement stmt =con.createStatement();
        ResultSet resl = stmt.executeQuery(query);

        while(resl.next()){
            System.out.println(resl.getString("Client Code"));
        }
    }
}

我的连接代码是

public Connection getConnection() {

    Connection con = null;

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String connectionUrl = "jdbc:odbc:SynergistConnection";

        con = DriverManager.getConnection(connectionUrl, "******", "******");
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("SQL Exception: " + e.getMessage());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: " + cE.toString()); 
    }
    return con;
}

有没有人遇到任何类似问题或知道如何解决这个问题?

堆栈跟踪:

java.lang.NullPointerException
at org.apache.jsp.test_005flaur_jsp._jspService(test_005flaur_jsp.java:124)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:0)

似乎你的getConnection()方法抛出异常。这就是为什么Connection对象 con 没有初始化的原因。因此,您将在以下行获取NullPointerException:

 Statement stmt =con.createStatement();

答案 1 :(得分:0)

Java 8中已删除sun.jdbc.odbc.JdbcOdbcDriver类。(请参阅this Answer。)

您编写的代码将捕获ClassNotFoundException(如果由上述原因引起),并返回null。 (坏,坏,坏......)

但来电者这样做了:

    Connection con = dbcon.getConnectionSynergist2();
    ...
    Statement stmt = con.createStatement();

假设getConnectionSynergist2getConnection方法(并且您错误地将代码复制到问题中),那么您可能会在{{1}上调用createStatement }}