随笔-94  评论-146  文章-0  trackbacks-0

 

Connection conn = getConnectionFromSomeplace(); conn.setAutoCommit(false); 
Statement stmt 
= conn.createStatement(); 
// Step 1: insert the person 
stmt.addBatch("INSERT INTO person (id, f_name, l_name) " + "VALUES (" + id + ", '" + firstName + "', '" + lastName + "')");
// Step 2: insert the person's account 
stmt.addBatch("INSERT INTO account (personID, type) " + "VALUES (" + id + ", 'SAVINGS')"); 
// Execute the batch 
int[] results = stmt.executeBatch(); 
// Check the batched results boolean 
completeSuccess = true
for (int i=0; i<results.length; i++
if (results[i] >= 0 || results[i] ==Statement.SUCCESS_NO_INFO) 
else 
/* Something went wrong; alert user? */ 
completeSuccess 
= false
}
 
}
 
if (completeSuccess) 
conn.commit(); 
else conn.rollback();
先要关闭AutoCommit,然后根据数据库返回的结果手动进行提交或者回滚
posted on 2008-03-27 18:01 TiGERTiAN 阅读(305) 评论(0)  编辑  收藏 所属分类: Java

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: