try{
Class.forName("oracle.jdbc.driver.Oracle");
conn =
DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SXT","scott","tiger");
conn.setAutoCommit(false);//DML语句,设置不自动提交。默认为true。
stmt = conn.createStatement();
stmt.addBatch("insert into dept2 values (51,'500','haha')");
stmt.addBatch("insert into dept2 values (52,'500','haha')");
stmt.addBatch("insert into dept2 values (53,'500','haha')");
stmt.executeBatch();
conn.commit();//执行批语句
conn.setAutoCmmit(true);//设回默认true,恢复现场,因为上面设为false
}catch(ClassFoundException e){//处理异常,很完美的方法。
e.printStackTrace();
}catch(SQlException e){
e.printStackTrace();
try{
if(conn!=null)
{
conn.rollback();//回滚
conn.setAutoCommit(true);//设回默认true,注意两种设置
}
}catch (SQLException e1){
e1.printStackTrace();
}
}finally{
try{
if(stmt !=null)
stmt.close();
if(conn !=null)
conn.close();
}catch(SQLExption e){
e.printStackTrace();
}
}
}