在eclipse中连接数据库并进行数据库操作时出现空指针异常类java.lang.NullPointerException


java.lang.NullPointerException
加载驱动成功!
连接数据库成功!
at com.dao.BookDAO.create(BookDAO.java:24)
at servlets.Insert_Book_Servlet.doPost(Insert_Book_Servlet.java:48)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)



出现异常的位置(红色):

public void create(BookInfo book) throws Exception {
// TODO Auto-generated method stub
Connection con=null;
PreparedStatement pst=null;
try{
con=DbConnection.getDBconnection();
pst=con.prepareStatement(INSERT_SQL);
pst.setString(1, book.getBookID());
pst.setString(2, book.getBookName());
pst.setString(3, book.getAuthor());
pst.setDouble(4, book.getPrice());
pst.setString(5, book.getRemarks());
pst.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
DbConnection.closeDB(con, pst, null);
}
}

原因:在getDbConnection()方法中没有返回值或返回值为null

解决办法:

public static Connection getDBconnection()
{
try{
Class.forName(driverName);
Connection con=DriverManager.getConnection(dbUrl, userName, userPwd);
System.out.println("加载驱动成功!");
System.out.println("连接数据库成功!");
return con;  //在连接数据库时要记得返回con,否则会报空指针异常;上述异常之所以会发生就是因为我之前没有返回值
}catch(Exception e){
e.printStackTrace();
System.out.println("加载驱动失败!");
System.out.println("连接数据库失败!");
}
return null;
}



你可能感兴趣的:(在eclipse中连接数据库并进行数据库操作时出现空指针异常类java.lang.NullPointerException)