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 阅读(1831)
评论(1) 编辑 收藏 所属分类:
Java