谁帮我看下,为什么下面代码无法rollback??
package com.hwz.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HwzTest {
public static void main(String[] args) throws SQLException{
Statement st=null;
ResultSet rs=null;
Connection conn=null;
Conn co=new Conn();
try{
conn=Conn.getConn();
conn.setAutoCommit(false);
st=co.getSt();
String sql=null;
int age=0;
sql="update classname set age=age-1 where id=3";
st.executeUpdate(sql);
sql="select * from classname where id=4";
rs=st.executeQuery(sql);
if(rs.next()){
age=rs.getInt(3);
}
if(age>1)
throw new RuntimeException("年龄太大");
sql="update classname set age=age+1 where id=4";
st.executeUpdate(sql);
conn.commit();
}catch(SQLException e){
if(conn!=null){
conn.rollback();
throw e;
}
}finally{
co.getClose();
}
}
}
是哪边写错了吗??
package com.hwz.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HwzTest {
public static void main(String[] args) throws SQLException{
Statement st=null;
ResultSet rs=null;
Connection conn=null;
Conn co=new Conn();
try{
conn=Conn.getConn();
conn.setAutoCommit(false);
st=co.getSt();
String sql=null;
int age=0;
sql="update classname set age=age-1 where id=3";
st.executeUpdate(sql);
sql="select * from classname where id=4";
rs=st.executeQuery(sql);
if(rs.next()){
age=rs.getInt(3);
}
if(age>1)
throw new RuntimeException("年龄太大");
sql="update classname set age=age+1 where id=4";
st.executeUpdate(sql);
conn.commit();
}catch(SQLException e){
if(conn!=null){
conn.rollback();
throw e;
}
}finally{
co.getClose();
}
}
}
是哪边写错了吗??
我想问题应该出在这,你不妨Debug看看。
在catch是因为抛了异常才跳,不是得到对象就跳.