书上一段程序如下
--------------------------------------------------------------------------------
import java.sql.*;public class TansactionPairs{
public static void main(String args[]){
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=publish";
String user="jieking";
String password="0085529j225";
Connection con=null;
Statiment stmt;
PreparedStatement updateSales;
PreparedStatement updateTotal;
String updateString=
"update COFFEES"+"set SALES= ?where COF_NAME=?";
String updateStatement=
"update COFFEES"+"set TOTAL = TOTAL + ?where COF_NAME?";
String query="select COF_NAME.SALES,TOTAL from COFFEES";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(java.lang.ClassNotFoundException e){
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
try{
con=DriverManager.getConnection(url,user,password);
uodateSales=con.prepareStatement(updateString);
updateTotal=con.prepareStatement(updateStatement);
int[]salesForWeek={175,150,60,155,90};
String[]coffees={"Colombian","French_Roast","Espresso",
 "Colombian_Decaf","French_Roast_decaf"};
int len=coffees.length;
con.setAutoCommit(false);
for(int i=0;i<len;i++){
updateSales.setInt(1,salesForWeek[i]);
updateSales.setString(2,coffees[i]);
updateSales.ExecuteUpdate();
updateTotal.setInt(1,salesForWeek[i]);
updateTotal.setString(2,coffees[i]);
updateTotal.ExecuteUpdate();
con.commit();
}
con.setAuto.Commit(true);
updateSales.close();
updateTotal.close();
stmt=con.createStatement();
ResulrSet rs=stmt.ExecuteQuery(query);
while(rs.next()){
String c=rs.getString("COF_NAME");
int s=rs.getInt("Sales");
int t=rs.getInt("Total");
System.out.println(c+""+""s+""+t);
    }
stmt.close();
con.close();
}catch(SQLEXception e){
System.err.println("SQLException:"+ex.getMessage());
if(con!=null){
try{
System.err.print("Transaction is begin");
system.err.println("rolled back");
con.rollback();
}catch(SQLEXception e){
System.err.print("SQLEXception:");
System.err.println(except.getMessage());
}
}
}
}
}
-------------------------------------------------------------------------
我想问问下面这段是啥意思,setAutoCommit(false)是干啥的。for循环里是起什么作用的
-----------------------------------------------------------------------
int len=coffees.length;
con.setAutoCommit(false);
for(int i=0;i<len;i++){
updateSales.setInt(1,salesForWeek[i]);
updateSales.setString(2,coffees[i]);
updateSales.ExecuteUpdate();
updateTotal.setInt(1,salesForWeek[i]);
updateTotal.setString(2,coffees[i]);
updateTotal.ExecuteUpdate();
con.commit();
}
con.setAuto.Commit(true);
updateSales.close();
updateTotal.close();
stmt=con.createStatement();
ResulrSet rs=stmt.ExecuteQuery(query);
while(rs.next()){
String c=rs.getString("COF_NAME");
int s=rs.getInt("Sales");
int t=rs.getInt("Total");
System.out.println(c+""+""s+""+t);
    }
stmt.close();
con.close();
              }
---------------------------

解决方案 »

  1.   

    这句话就是不允许自动将操作结果提交给数据库,只有手动提交。设为true就是每次都直接将结果反映到数据库。
      

  2.   

    pstmt=conn.preparedStatement("insert into test_table(......) values(....?)"); pstmt.setString(1,"aaa"); pstmt.addBatch(); pstmt.setString(1,"bbb"); pstmt.addBatch(); ..... pstmt.executeBatch(); 
    批量地从数据库中取数据.