tacktrace:
java.lang.NullPointerException
at com.util.DBO.close(DBO.java:57)
at com.bean.SystemBean.getSiteInfo(SystemBean.java:74)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:148)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
代码:
package com.util;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.activation.DataSource;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
public class DBO {
private Connection conn;
private Statement stmt;
private DataSource ds;
public DBO()
{
}
/**
打开数据库
*/
public void open()
{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jf?useSSL=false&serverTimezone=UTC","root","wangbo321");
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=xswz","sa","123");
stmt=conn.createStatement();
System.out.println("打开数据库连接");
}
catch (Exception ex)
{
System.err.println("打开数据库时出错: " + ex.getMessage());
}
}
/**
关闭数据库,将连接返还给连接池
*/
public void close()
{
try
{
// connMgr.freeConnection("java", conn);
conn.close();
System.out.println ("释放连接");
}
catch (SQLException ex)
{
System.err.println("返还连接池出错: " + ex.getMessage());
}
}
/**
执行查询
*/
public ResultSet executeQuery(String sql) throws SQLException
{
ResultSet rs = null;
rs = stmt.executeQuery(sql);
System.out.println ("执行查询");
return rs;
}
/**
执行增删改
*/
public int executeUpdate(String sql) throws SQLException
{
int ret = 0;
ret = stmt.executeUpdate(sql);
System.out.println ("执行增删改");
return ret;
}
/**
将SQL语句加入到批处理
*/
public void addBatch(String sql) throws SQLException
{
stmt.addBatch(sql);
}
/**
执行批处理
*/
public int [] executeBatch() throws SQLException
{
boolean isAuto=conn.getAutoCommit();
conn.setAutoCommit(false);
int [] updateCounts = stmt.executeBatch();
// conn.commit();
// conn.setAutoCommit(isAuto);
//conn.setAutoCommit(true);
return updateCounts;
}
public boolean getAutoCommit() throws SQLException
{
return conn.getAutoCommit();
}
public void setAutoCommit(boolean auto) throws SQLException
{
conn.setAutoCommit(auto);
}
public void commit() throws SQLException
{
conn.commit();
// this.close();
}
public void rollBack() throws SQLException
{
conn.rollback();
// this.close();
}
}
这个方法里面空指针异常,57行。至于为什么空指针,没人知道。
有可能是Open的时候,发生了异常。导致conn对象是null。