JAVA里怎么开始一个新的事务啊? 我要起用事务处理呀。在JAVA里要用什么方法?如果什么也不设,JDBC默认的事务级别是什么?这还不够清楚啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Database.commit和Database.rollbackJDBC默认的事务级别是自动提交,即执行完语句后,自动对那个语句调用commit()方法 事务处理有呀!上面的那位不是说了嘛,不过要数据库支持,比如MYSQL就没有事务处理! 我是问如何启动一个事务啊。事务还没启动,怎么能COMMIT啊?启动事务的函数是什么? 该你个完整的例子import java.sql.*;public class BatchUpdate { public static void main(String args[]) throws SQLException { ResultSet rs = null; PreparedStatement ps = null; String url = "jdbc:mySubprotocol:myDataSource"; Connection con; Statement stmt; try { Class.forName("myDriver.ClassName"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); con.setAutoCommit(false); stmt = con.createStatement(); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto', 49, 9.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut', 49, 9.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto_decaf', 49, 10.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)"); int [] updateCounts = stmt.executeBatch(); con.commit(); con.setAutoCommit(true); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String name = uprs.getString("COF_NAME"); int id = uprs.getInt("SUP_ID"); float price = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int total = uprs.getInt("TOTAL"); System.out.print(name + " " + id + " " + price); System.out.println(" " + sales + " " + total); } uprs.close(); stmt.close(); con.close(); } catch(BatchUpdateException b) { System.err.println("-----BatchUpdateException-----"); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor: " + b.getErrorCode()); System.err.print("Update counts: "); int [] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } System.err.println(""); } catch(SQLException ex) { System.err.println("-----SQLException-----"); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor: " + ex.getErrorCode()); } }} 你看看书吧,当你建立连接后,一个事务就开始了,当你调用了Connect 的Commit()或Rollback()方法时,你的事务就提交了,这个事务完成,同时下一个事务又开始了。只是当你用自动提交时,每发送一次SQL调用都会提交一次,结束一个事务,同时开始下一个事务你的另一个问题是事务隔离级别,可以参考JDBC和相关的数据库设定。当然,上面的关于事务的前提就是数据库要支持事务,否则免谈。 自动提交时,每发送一次SQL调用系统都会自动提交一次,这是数据库的特性。 用JAVA 怎么实现 对电脑 功率的 监控 ?。。。。。。。。。。。。。。。。。。。。人工加长 同步和异步有何异同,在什么情况下分别使用他们?举例说明。 认为自己很聪明的请进 java+MS Sql编写应用程序方不方便呀? 急招网站开发程序员(J2EE) 面试题 30分 程序问题请进在线等 jbuild中能不能象微软vb,vc,.net一样,出错时可以停在出错的地方,类似在vb中break on all Errors 菜鸟一问 together在j2sdk1.4.0下的安装问题-----200分求救,分次给 JInternalWindow的一些问题,(希望大家讨论) 中国java现状如何?
JDBC默认的事务级别是自动提交,即执行完语句后,自动对那个语句调用commit()方法
事务还没启动,怎么能COMMIT啊?
启动事务的函数是什么?
import java.sql.*;
public class BatchUpdate {
public static void main(String args[]) throws SQLException { ResultSet rs = null;
PreparedStatement ps = null;
String url = "jdbc:mySubprotocol:myDataSource";
Connection con;
Statement stmt;
try {
Class.forName("myDriver.ClassName");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"myLogin", "myPassword");
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Amaretto', 49, 9.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Hazelnut', 49, 9.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Amaretto_decaf', 49, 10.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)");
int [] updateCounts = stmt.executeBatch();
con.commit();
con.setAutoCommit(true);
ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
System.out.println("Table COFFEES after insertion:");
while (uprs.next()) {
String name = uprs.getString("COF_NAME");
int id = uprs.getInt("SUP_ID");
float price = uprs.getFloat("PRICE");
int sales = uprs.getInt("SALES");
int total = uprs.getInt("TOTAL");
System.out.print(name + " " + id + " " + price);
System.out.println(" " + sales + " " + total);
}
uprs.close();
stmt.close();
con.close();
} catch(BatchUpdateException b) {
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
System.err.println(""); } catch(SQLException ex) {
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}