|
|
|
|
|
|
|
帮我一下吧,拜托了
|
2004年11月11日 22:53
|
|
|
标签列表
|
|
我在同一个函数中使用两个连接一个更新,一个查询 为什么得不到更新后的结果 (得到的结果总是制后更新一步) package untitled1; import java.sql.*; public class Untitled1 { public static void main(String[] args) { String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"; String sConnStr="jdbc:odbc:cityLayoutManager"; String sql="";
Connection conn=null; ResultSet rs=null; Statement stmt=null;
Connection conn2=null; ResultSet rs2=null; Statement stmt2=null;
try{
Class.forName(sDBDriver); conn=DriverManager.getConnection(sConnStr); stmt=conn.createStatement(); }catch(java.lang.ClassNotFoundException e){ System.err.println(e.getMessage()); }catch(java.sql.SQLException e1) { System.err.println(e1.getMessage());}
try{ //原来为66666666666666 sql="update test set mydate='77777777777'"; stmt.executeUpdate(sql);
}catch(Exception e) { e.printStackTrace(); }
try{ Class.forName(sDBDriver); conn2=DriverManager.getConnection(sConnStr); stmt2=conn2.createStatement(); }catch(java.lang.ClassNotFoundException e){ System.err.println(e.getMessage()); }catch(java.sql.SQLException e1) { System.err.println(e1.getMessage());}
try{ sql="select * from test"; rs2=stmt2.executeQuery(sql); if(rs2.next()) System.out.println(rs2.getString(1)); stmt.close(); conn.close(); stmt2.close(); conn2.close(); }catch(Exception e) { e.printStackTrace(); } } } 我得到的却是66666666666666 每次执行得到的都是上一次的结果
|
|
|
|
|
|
Re: 帮我一下吧,拜托了
|
2004年11月15日 17:50
|
|
|
你使用了两个连接: 第一个连接执行的更新尚未提交。第二个连接的查询当然看见的是没有更新的数据。
|
|
|
|
|
|
Re: 帮我一下吧,拜托了
|
2004年11月16日 22:01
|
|
|
stmt.executeUpdate(sql); 语句不是提交查询吗?
|
|
|
|
|
|
Re: 帮我一下吧,拜托了
|
2004年11月17日 13:35
|
|
|
1、在更新语句后面加conn.commit试一下(一般来说这句没有必要,因为默认auto-commit 属性为true); 2、其实连接没有必要创建两个,一个足已,多了是浪费资源,影响性能,更新和查询可以用同一个,你不防试一下; 3、关闭连接最好放在finally中,这样可能确保用完之后连接一定会被关闭。在关闭连接前也要把结果集和创建的语句关掉。 不妨试一试!祝好运!
|
|
|
|
|
|
Re: 帮我一下吧,拜托了
|
2004年11月17日 16:48
|
|
|
|
需要重构一下,分成两个方法,每个方法需要finnaly中实现stmt.close();conn.close();
|
|
|
|
|
|
Re: 帮我一下吧,拜托了
|
2004年11月18日 21:24
|
|
|
|
但是我在项目中使用了自己写的数据库缓冲池,连接是不会被关闭的,也就是说,数据库会总是会制后修改一步了
|
|
|
|