数据库连接rs/stmt不关闭,只关闭conn会出现什么问题!

zxp_net 2003-10-21 09:27:00
Connection conn = ds.getConnection();
statment stmt = conn.........
ResultSet rs = stmt.............
rs.excute();


conn.close();

这样会出现什么问题,为什么呢??
...全文
221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxp_net 2003-10-21
  • 打赏
  • 举报
回复
大家是否遇到过这种异常:
Error Message: Connection reset by peer: socket write error
Error Code: 500
Target Servlet: null
Error Stack:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite(Native Method)
at java.net.SocketOutputStream.write(SocketOutputStream.java(Compiled Code))
at com.ibm.ws.io.Stream.write(Stream.java(Compiled Code))
fengyuxi 2003-10-21
  • 打赏
  • 举报
回复
正确的释放资源顺序应该是:
result.close();
statement.close();
connection.close();
lqz790815 2003-10-21
  • 打赏
  • 举报
回复
感觉这样比较合适:
if ( rs != null )
{
try
{
rs.close () ;
}
catch ( Exception ex )
{}
}

if ( stm != null )
{
try
{
stm.close () ;
}
catch ( Exception ex )
{}
}

if ( conn != null )
{
try
{
conn.close () ;
System.out.println ( "conn.close" ) ;
}
catch ( Exception ex )
{}
}
vcshcn 2003-10-21
  • 打赏
  • 举报
回复
最好再加上rs = null; stmt = null;
zxp_net 2003-10-21
  • 打赏
  • 举报
回复
CoolAbu:好像是这样的。但为什么都喜欢用rs/stmt.close()呢,用与不用有什么区别,那种用法好些
fengyuxi 2003-10-21
  • 打赏
  • 举报
回复
关键是:
数据库cursor资源将被慢慢耗尽!
CoolAbu 2003-10-21
  • 打赏
  • 举报
回复
rs的资源没有释放,等待Garbage Collector来收集。
zxp_net 2003-10-21
  • 打赏
  • 举报
回复
不可能,记录能取出,能执行,但不知道资源是否释放。等等......
lmh7607 2003-10-21
  • 打赏
  • 举报
回复
記錄取不出吧.
这个是一个连接MySQL数据库的操作类 public class MyJdbc { public static void main(String[] args) { // register(); display(); } private static void display() { // 定义链接字符串 String connectionString = "jdbc:mysql://localhost:3306/blog?user=root&password=root"; // 定义驱动数据库的类 String driver = "org.gjt.mm.mysql.Driver"; // 定义连接数据库对象conn Connection conn = null; // 定义执行sql语句对象stmt Statement stmt = null; //定义结果集对象rs,用来保存查询的结果 ResultSet rs = null; try { // 通过反射加载驱动程序类 Class.forName(driver).newInstance(); // Connection作用是连接数据库 conn = DriverManager.getConnection(connectionString); String sql = "select * from userinfo"; // Statement作用是向数据库中发送sql语句,并告诉数据库执行sql语句,还要让数据库返回执行的结果 stmt = conn.createStatement(); // 向数据库中发送sql,并执行,获取执行结果 rs = stmt.executeQuery(sql); //获取结果集架构信息(有多少列,每列叫什么名字) ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { System.out.print(rsm.getColumnName(i)); System.out.print("\t"); } System.out.println(); while(rs.next()){ System.out.print(rs.getInt("userid")); System.out.print("\t"); System.out.print(rs.getString("loginId")); System.out.print("\t"); System.out.print(rs.getString("loginPwd")); System.out.print("\t"); System.out.println(); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放系统资源 try { if(rs!=null){ rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void register() { Scanner input = new Scanner(System.in); // 定义链接字符串 String connectionString = "jdbc:mysql://localhost:3306/blog?user=root&password=root"; // 定义驱动数据库的类 String driver = "org.gjt.mm.mysql.Driver"; // 定义连接数据库对象conn Connection conn = null; // 定义执行sql语句对象stmt Statement stmt = null; try { // 通过反射加载驱动程序类 Class.forName(driver).newInstance(); // Connection作用是连接数据库 conn = DriverManager.getConnection(connectionString); // 定义sql语句 System.out.println("请输入用户名"); String loginId = input.next(); System.out.println("请输入密码"); String loginPwd = input.next(); StringBuffer sqlbuffer = new StringBuffer(); sqlbuffer .append("INSERT INTO UserInfo (loginId,loginPwd) VALUES('"); sqlbuffer.append(loginId); sqlbuffer.append("','"); sqlbuffer.append(loginPwd); sqlbuffer.append("')"); String sql = sqlbuffer.toString(); // String sql ="INSERT INTO UserInfo (loginId,loginPwd) VALUES('"+ // loginId +"','"+loginPwd+"')"; // Statement作用是向数据库中发送sql语句,并告诉数据库执行sql语句,还要让数据库返回执行的结果 stmt = conn.createStatement(); // 向数据库中发送sql,并执行,获取执行结果 int i = stmt.executeUpdate(sql); if (i > 0) { System.out.println("注册成功"); } else { System.out.println("注册失败"); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放系统资源 try { if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }
经典java数据库封装类,package com.bjsxt.shopping.util; import java.sql.*; public class DB { public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static Statement getStatement(Connection conn) { Statement stmt = null; try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } /* public static ResultSet getResultSet(Connection conn, String sql) { Statement stmt = getStatement(conn); ResultSet rs = getResultSet(stmt, sql); close(stmt); return rs; } */ public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Statement stmt, String sql) { try { if(stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧