try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","userpwd";
conn.setAutoCommit(false);//禁止自动提交,设置回滚点
stmt = conn.createStatement();
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}catch(Exception e1){}
try{
stmt.executeUpdate(“insert into table …”); //数据库更新操作2
}catch(Exception e2){}
conn.commit(); //事务提交
}catch(Exception ex) {
try {
conn.rollback(); //操作不成功则回滚
}catch(Exception e) {
e.printStackTrace();
}
ex.printStackTrace();
}
我想知道的是:
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}
发生异常的话,程序还会走到conn.rollback()上吗?
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","userpwd";
conn.setAutoCommit(false);//禁止自动提交,设置回滚点
stmt = conn.createStatement();
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}catch(Exception e1){}
try{
stmt.executeUpdate(“insert into table …”); //数据库更新操作2
}catch(Exception e2){}
conn.commit(); //事务提交
}catch(Exception ex) {
try {
conn.rollback(); //操作不成功则回滚
}catch(Exception e) {
e.printStackTrace();
}
ex.printStackTrace();
}
我想知道的是:
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}
发生异常的话,程序还会走到conn.rollback()上吗?
解决方案 »
- .jar 文件的打开问题
- 在线等。魔术方阵算法,高手进
- 有个综合的问题急须解决,是关于JAVA基础知识的,请高手来帮我解决,我在线等待
- 如何在JTextArea中实现撤消功能
- jtable 单元格颜色 烦
- 请问谁有Java连接到SQL Server数据库的完整程序? ...
- 请问vector的容量是否有限制?
- 如何发布自己编写的包?
- 怎样在windows下把 " \" 转换为"\\"存入mysql数据库中,多谢!!
- 听说新出的JB6是4张盘,是吗?都是什么内容?大家讨论一下好吗?
- Process对象里面的getInputStream(),getOutputStream()是用来做什么的?
- 真奇怪,JTextArea的滚动条还是没有出现?
当alter发生异常的时候,什么都不会发生,因为你catch中什么也没有写。然后代码下移到insert
显然不会! ///////////////////////////////////////////////////////////////////////////
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}catch(Exception e1){}
try{
stmt.executeUpdate(“insert into table …”); //数据库更新操作2
}catch(Exception e2){}
///////////////////////////////////////////////////////////////////////////
stmt.executeUpdate(“alter table …”); //数据库更新操作1
stmt.executeUpdate(“insert into table …”); //数据库更新操作2 这两处有可能发生的异常都已经被catch(Exception e1){}、catch(Exception e2){}捕获了,又没有被重新抛出,不管有没有发生都不会把异常传给上层 因此
try{
stmt.executeUpdate(“alter table …”); //数据库更新操作1
}
发生异常的不会使程序还会走到conn.rollback()上 ///////////////////////////////////////////////////////////////////////////
conn = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:SID","username","userpwd");
conn.setAutoCommit(false);//禁止自动提交,设置回滚点
stmt = conn.createStatement();
...
conn.commit(); //事务提交
///////////////////////////////////////////////////////////////////////////
如果上面的代码段发生异常,则会使程序还会走到conn.rollback()上
如果
try{
stmt.executeUpdate(“insert into table …”); //数据库更新操作2
}catch(Exception e2){}
发生异常,那么程序既没有执行commit也没有执行rollback,那是不是conn就不会被释放?
上面的程序从优化的角度来讲存在那些问题。谢谢!