随笔-348  评论-598  文章-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,然后根据数据库返回的结果手动进行提交或者回滚

---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2008-03-27 18:01 TiGERTiAN 阅读(1828) 评论(1)  编辑  收藏 所属分类: Java

评论:
# re: Java中批处理SQL的使用方法(JDBC) 2009-04-20 14:36 | 百林
dskljfsafhuiefhuailf  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: