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 閱讀(1834)
評論(1) 編輯 收藏 所屬分類:
Java