我要起用事务处理呀。
在JAVA里要用什么方法?
如果什么也不设,JDBC默认的事务级别是什么?
这还不够清楚啊?

解决方案 »

  1.   

    Database.commit和Database.rollback
    JDBC默认的事务级别是自动提交,即执行完语句后,自动对那个语句调用commit()方法
      

  2.   

    事务处理有呀!上面的那位不是说了嘛,不过要数据库支持,比如MYSQL就没有事务处理!
      

  3.   

    我是问如何启动一个事务啊。
    事务还没启动,怎么能COMMIT啊?
    启动事务的函数是什么?
      

  4.   

    该你个完整的例子
    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());
    }
    }
    }
      

  5.   

    你看看书吧,当你建立连接后,一个事务就开始了,当你调用了Connect 的Commit()或Rollback()方法时,你的事务就提交了,这个事务完成,同时下一个事务又开始了。只是当你用自动提交时,每发送一次SQL调用都会提交一次,结束一个事务,同时开始下一个事务你的另一个问题是事务隔离级别,可以参考JDBC和相关的数据库设定。当然,上面的关于事务的前提就是数据库要支持事务,否则免谈。
      

  6.   

    自动提交时,每发送一次SQL调用系统都会自动提交一次,这是数据库的特性。