package entity;
import util.ConnectionUtils;
import java.sql.*;
public class Category {
public void add(String name){
Connection conn = null;
Statement stmt = null;
try{
conn = ConnectionUtils.getConnection();
String sql = "insert into news_class (name) values('"+name+"')";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}finally{
if(stmt != null){
try{
stmt.close();
stmt = null;
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
conn=null;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
No operations allowed after connection closed.现在是报这个错误。
数据库不是操作完就是close吗?
第二次访问的时候就提示这个异常了。怎么解决啊。?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以关闭操作stmt.close();,但是关闭连接conn.close();,就不能再操作了,要想继续操作就得重新connect
下次重新连接呗
你把ConnectionUtils.getConnection()代码贴一下,是不是每次调用都开一个连接,还是返回一个静态的只初始化一次的connection,如果是只初始化一次,那不要close connection了,因为第二次调用获得的就是一个已经关闭的连接了