社区
Java EE
帖子详情
数据库连接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();
这样会出现什么问题,为什么呢??
...全文
222
9
打赏
收藏
数据库连接rs/stmt不关闭,只关闭conn会出现什么问题!
Connection conn = ds.getConnection(); statment stmt = conn......... ResultSet rs = stmt............. rs.excute(); conn.close(); 这样会出现什么问题,为什么呢??
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
記錄取不出吧.
数据库连接
代码
Conn
ection
conn
= null;//声明
数据库连接
对象 Statement
stm
t = null; //声明数据库表达式对象 ResultSet
rs
= null;//声明结果集对象 try { // 载入Mysql的驱动字符串 Class.forName("com.mysql.jdbc.Driver"); // 获取数据库的连接
conn
= DriverManager.get
Conn
ection("jdbc:mysql://localhost:3306/use
rs
", "root", "123456"); // 获取表达式对象实例
stm
t =
conn
.createStatement();
rs
=
stm
t.executeQuery("select * from user where username='"+user+"'and password='"+pass+"'"); if(
rs
.next()) { out.println("你终于登录成功啦,啊啊啊啊啊啊啊"+user);} else { out.println("你登录失败啦,检查你的用户名和密码吧"); } } catch (Exception e) { e.printStackTrace(); } finally {
jdbc连接代码
这个是一个连接MySQL数据库的操作类 public class MyJdbc { public static void main(String[] args) { // register(); display(); } private static void display() { // 定义链接字符串 String
conn
ectionString = "jdbc:mysql://localhost:3306/blog?user=root&password=root"; // 定义驱动数据库的类 String driver = "org.gjt.mm.mysql.Driver"; // 定义连接数据库对象
conn
Conn
ection
conn
= null; // 定义执行sql语句对象
stm
t Statement
stm
t = null; //定义结果集对象
rs
,用来保存查询的结果 ResultSet
rs
= null; try { // 通过反射加载驱动程序类 Class.forName(driver).newInstance(); //
Conn
ection作用是连接数据库
conn
= DriverManager.get
Conn
ection(
conn
ectionString); String sql = "select * from userinfo"; // Statement作用是向数据库中发送sql语句,并告诉数据库执行sql语句,还要让数据库返回执行的结果
stm
t =
conn
.createStatement(); // 向数据库中发送sql,并执行,获取执行结果
rs
=
stm
t.executeQuery(sql); //获取结果集架构信息(有多少列,每列叫什么名字) ResultSetMetaData
rs
m =
rs
.getMetaData(); for (int i = 1; i <=
rs
m.getColumnCount(); i++) { System.out.print(
rs
m.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 (
stm
t != null) {
stm
t.close();
stm
t = null; } if (
conn
!= null) {
conn
.close();
conn
= null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void register() { Scanner input = new Scanner(System.in); // 定义链接字符串 String
conn
ectionString = "jdbc:mysql://localhost:3306/blog?user=root&password=root"; // 定义驱动数据库的类 String driver = "org.gjt.mm.mysql.Driver"; // 定义连接数据库对象
conn
Conn
ection
conn
= null; // 定义执行sql语句对象
stm
t Statement
stm
t = null; try { // 通过反射加载驱动程序类 Class.forName(driver).newInstance(); //
Conn
ection作用是连接数据库
conn
= DriverManager.get
Conn
ection(
conn
ectionString); // 定义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语句,还要让数据库返回执行的结果
stm
t =
conn
.createStatement(); // 向数据库中发送sql,并执行,获取执行结果 int i =
stm
t.executeUpdate(sql); if (i > 0) { System.out.println("注册成功"); } else { System.out.println("注册失败"); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放系统资源 try { if (
stm
t != null) {
stm
t.close();
stm
t = null; } if (
conn
!= null) {
conn
.close();
conn
= null; } } catch (SQLException e) { e.printStackTrace(); } } } }
java数据库封装类
经典java数据库封装类,package com.bjsxt.shopping.util; import java.sql.*; public class DB { public static
Conn
ection get
Conn
() {
Conn
ection
conn
= null; try { Class.forName("com.mysql.jdbc.Driver");
conn
= DriverManager.get
Conn
ection("jdbc:mysql://localhost/shopping?user=root&password=root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return
conn
; } public static PreparedStatement prepare(
Conn
ection
conn
, String sql) { PreparedStatement p
stm
t = null; try { if(
conn
!= null) { p
stm
t =
conn
.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return p
stm
t; } public static PreparedStatement prepare(
Conn
ection
conn
, String sql, int autoGenereatedKeys) { PreparedStatement p
stm
t = null; try { if(
conn
!= null) { p
stm
t =
conn
.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return p
stm
t; } public static Statement getStatement(
Conn
ection
conn
) { Statement
stm
t = null; try { if(
conn
!= null) {
stm
t =
conn
.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return
stm
t; } /* public static ResultSet getResultSet(
Conn
ection
conn
, String sql) { Statement
stm
t = getStatement(
conn
); ResultSet
rs
= getResultSet(
stm
t, sql); close(
stm
t); return
rs
; } */ public static ResultSet getResultSet(Statement
stm
t, String sql) { ResultSet
rs
= null; try { if(
stm
t != null) {
rs
=
stm
t.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return
rs
; } public static void executeUpdate(Statement
stm
t, String sql) { try { if(
stm
t != null) {
stm
t.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(
Conn
ection
conn
) { try { if(
conn
!= null) {
conn
.close();
conn
= null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement
stm
t) { try { if(
stm
t != null) {
stm
t.close();
stm
t = 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(); } } }
jsp 用内置对象编写的留言板
try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//装载驱动 String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";//建立连接字符串
Conn
ection
conn
=DriverManager.get
Conn
ection(url,"sa","123");//建立连接 if(
conn
!=null) out.print("
conn
ection!!"); Statement
stm
t=
conn
.createStatement();//创建表达式对象 String sql="select user(),now()";//定义数据库操作 ResultSet
rs
=
stm
t.executeQuery(sql);//执行操作返回结果 while (
rs
.next()){ out.print("sqluser:"+
rs
.getString(1)+"
"); out.print("nowtime:"+
rs
.getString(2)+"
"); }
rs
.close();
stm
t.close();
conn
.close(); }catch(ClassNotFoundException cnfe){ out.print(cnfe); }catch(SQLException sqle){ out.print(sqle); }catch(Exception e){ out.print(e); }
数据库连接
类
连接类public void close(
Conn
ection
conn
, Statement
stm
t, ResultSet
rs
) { close(
rs
); close(
stm
t); close(
conn
); }
Java EE
67,512
社区成员
225,880
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章