com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
错误是这样的,如果我不对Connection进行关闭,则不会出错,而如果我加了conn.close();之后,下一次再进来这边就会报错
大家帮帮我
/**
* 获取按钮事件
*
* @return
* @throws SQLException
*/
public List<AlarmData> getNewButtonStat2() throws SQLException {
List<AlarmData> list = new ArrayList<AlarmData>();
Connection conn = MySQLDBUtil.getConn();
Statement statement = conn.createStatement();
// 通过查询返回结果
ResultSet rs = statement
.executeQuery("SELECT id,bid,keyid,ttime,yn,status FROM(SELECT * FROM t_alarmdata WHERE bid BETWEEN 9 AND 12 ORDER BY ttime DESC LIMIT 2000) a GROUP BY a.bid,a.keyid");
// 循环取出 rs 中的结果
while (rs.next()) {
AlarmData ad = new AlarmData();
ad.setBid(rs.getString("bid"));
ad.setId(rs.getInt("id"));
ad.setKeyid(rs.getString("keyid"));
ad.setStatus(rs.getInt("status"));
ad.setYn(rs.getInt("yn"));
list.add(ad);
}
rs.close();
statement.close();
conn.close(); // 如果这里的不关闭,则没有错误信息,加上conn.close(); 下次再进来这里就会报如下错误
System.out.println(list.size());
return list;
}
MySQLDBUtil 数据库链接类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDBUtil {
// 本机
private static final String URL = "jdbc:mysql://127.0.0.1:3306/warning_light";
private static final String USER = "root";
private static final String PASSWORD = "888888";
private static Connection conn = null;
DBPool dbp = null;
static {
// 1.加载驱动(反射机制)
try {
Class.forName("com.mysql.jdbc.Driver");
// 2.获得数据库连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("sorry,can't find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println(conn);
e.printStackTrace();
}
}
public MySQLDBUtil(){
}
public static Connection getConn() {
return conn;
}
}
错误信息
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1098)
at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2380)
at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2362)
// 第一段代码的11行
at com.lewei.dao.WarningLightDao.getNewButtonStat2(WarningLightDao.java:137)// 第一段代码的11行
at com.lewei.www.WarningScreen$6.doInBackground(WarningScreen.java:983)
at com.lewei.www.WarningScreen$6.doInBackground(WarningScreen.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
------解决思路----------------------
刚刚看到你的connection是static的。不可以把connection 定义为static, 只能是成员变量
------解决思路----------------------
static {
// 1.加载驱动(反射机制)
try {
Class.forName("com.mysql.jdbc.Driver");
// 2.获得数据库连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("sorry,can't find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println(conn);
e.printStackTrace();
}
}
你这里面的静态代码块只会在MySQLDBUtil类第一次调用的时候被执行,也就是说conn只在你第一次调用Connection conn = MySQLDBUtil.getConn();时候得到连接,你把它close了就不会再执行conn = DriverManager.getConnection(URL, USER, PASSWORD);获取连接了