Java代码增加回滚6_为什么在我的java代码中出现异常后没有完成回滚操作

我有一个jdbc代码,我在代码中使用了事务管理。

以下是代码。我正在使用Mysql数据库。

public class JdbcConn {

public static void main(String[] args){

Savepoint spt1 = null;

Connection con = null;

try{

Class.forName("org.gjt.mm.mysql.Driver");

con = DriverManager.getConnection("jdbc:mysql://localhost","root","tany");

con.setAutoCommit(false);

spt1= con.setSavepoint("svpt1");

PreparedStatement psmt;

String query1 = "select city, countryid from querytest.city;";

psmt=con.prepareStatement(query1);

ResultSet rs=psmt.executeQuery();

while(rs.next()){

String query2 = "insert into sun.city (city,countryid) values('"+rs.getString(1)+"',"+rs.getInt(2)+");";

psmt=con.prepareStatement(query2);

psmt.executeUpdate();

}

String query3 = "create database `transtest`;";

psmt=con.prepareStatement(query3);

psmt.executeUpdate();

String query4 = "CREATE TABLE `transtest`.`trans` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";

psmt=con.prepareStatement(query4);

psmt.executeUpdate();

String query5 = "CREATE TABLE `transtest`.`transone` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";

psmt=con.prepareStatement(query5);

psmt.executeUpdate();

String query6 = "CREATE TABLE `transtest`.`transtwo` (`id` tinyint(4) NOT NULL auto_increment,`val` int(5) NOT NULL default 0, PRIMARY KEY (`id`)) ENGINE=MyISAM;";

psmt=con.prepareStatement(query6);

psmt.executeUpdate();

for(int i=1;i<=10;i++){

String query7 = "insert into `transtest`.`transtwo` (`val`) values ("+i*2+");";

psmt=con.prepareStatement(query7);

psmt.executeUpdate();

}

String query8 = "insertd into `transtest`.`trans` (`val`) values (500);";

psmt=con.prepareStatement(query8);

psmt.executeUpdate();

JOptionPane.showMessageDialog(null, "Process completed!");

con.commit();

con.setAutoCommit(true);

}catch(SQLException sqle){

try {

con.rollback(spt1);

JOptionPane.showMessageDialog(null, "Rollback1!");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

sqle.getMessage();

sqle.printStackTrace();

}catch (ClassNotFoundException cnfe) {

// TODO Auto-generated catch block

try {

con.rollback(spt1);

JOptionPane.showMessageDialog(null, "Rollback2!");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

cnfe.getMessage();

cnfe.printStackTrace();

}catch (Exception e) {

// TODO Auto-generated catch block

try {

con.rollback(spt1);

JOptionPane.showMessageDialog(null, "Rollback3!");

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

e.getMessage();

e.printStackTrace();

}

}}

当sql异常出现时,上面的代码不会回滚。

query1中表的模式和query2中表的模式是相同的,但正如您所看到的数据库不同。

我只是不知道是否有任何例外,为什么它不将queryer所做的更改从query2回滚到query7。

我曾经在query8中意外地犯了一个例外的语法错误。

请在这个问题上指导我的朋友,并让我知道我的代码错误。

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值