private Connection connection;
private Statement pre;
public String set(String goal,String marriage){
return "update info set goal='"+goal+"'where marriage='"+marriage+"'";
}
public void start(){
String sql="jdbc:mysql://127.0.0.1/student";
try {
connection = DriverManager.getConnection(sql,"root","111");
connection.setAutoCommit(false);
pre = connection.createStatement();
pre.executeUpdate(set("配偶","未婚"));
pre.executeUpdate(set("情人","已婚"));
pre.executeUpdate(set("包养","学生"));
if(true)
throw new SQLException("ni");
connection.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
抛的是什么异常,发一下
if(true)
throw new SQLException("ni");
这个自定义异常,一定会抛出。。。。。但-----它是上抛,相当于方法抛出异常一样,向上级(main方法)抛,所以try-catch中,
不会捕获到你的自定义SQLException("ni");
复制的这部分代码不会提交sql,我试过了
抛了异常是不会提交sql 的,,,,