addBatch

在批量更新 SQL 操作的時候建議使用 addBatch,這樣效率是高些,數據量越大越能體現出 來


Statement 接口裏有兩個方法:
void addBatch addBatch(String sql)
將給定的 SQL 命令添加到此 Statement 對象的當前命令列表中。通過調用方 法 executeBatch 可以批量執行此列表中的命令。


int[] executeBatch executeBatch() 將一批命令提交給數據庫來執行,如果全部命令執行成功, 則返回更新計數組成的數組。 返回: 返回: 包含批中每個命令的一個元素的更新計數所組成的數組 (數組中 的每個元素爲: 成功處理了命令後,執行命令所影響數據庫中行數的更 新計數)。數組的元素根據將命令添加到批中的順序排序。


PreparedStatement 接口裏:重寫了 addBatch()的方法,executeBatch executeBatch()並沒有重寫
void addBatch addBatch() <注意:PreparedStatement 的 addBatch( )沒有參數的> 將一組參數添加到此 PreparedStatement 對象的批處理命令中。
PreparedStatement.addbatch()的使用:
JDBC 批量更新 pstmt.addBatch();的問題 在數據量越大的時候 越能體現 addBatch()的優勢 因爲數據庫的處理速度是非常驚人的 單次吞吐量很大 執行效率極高 addBatch()把若干 sql 語句裝載到一起,然後一次送到數據庫執行,執行需要很 短的時間 而 pstmt.executeUpdate() 是一條一條發往數據庫執行的 時間都消耗在數據 庫連接的傳輸上面 舉個例子可以幫助理解: 我這有一臺超大功率的麪粉加工機,前者相當於 把所有農戶袋裝的麥子收集起 來用卡車一次送往加工廠 後者相當於農戶排好隊用同樣的卡車一人一人的往加 工廠送麥子 麥子加工 5 分鐘完成,但是每個人到工廠就得 3 小時,我數據庫執 行效率再高也沒用,時間都耗在傳輸的路上了!! 這就出現了數據傳輸的性能瓶頸 addBatch 就是爲解決這樣的問題而產生的!


Statement 和 PreparedStatement 的區別就不多廢話了,直接說 PreparedStatement 最重要的 addbatch()結構的使用.

1.建立鏈接,(打電話撥號 ) Connection connection =getConnection();
2.不自動 Commit (瓜子不是一個一個喫,全部剝開放桌子上,然後一口舔了) connection.setAutoCommit(false);

3.預編譯 SQL 語句,只編譯一回哦,效率高啊.(發明一個剝瓜子的方法,以後不要 總想怎麼剝瓜子好.就這樣剝.) PreparedStatement statement = connection.prepareStatement("INSERT INTO TABLEX VALUES(?, ?)");

 4.來一個剝一個,然後放桌子上 //記錄 1 statement.setInt(1, 1); statement.setString(2, "Cujo"); statement.addBatch();//把這條執行語句加到 PreparedStatement 對象的批處理命令中 //記錄 2 statement.setInt(1, 2); statement.setString(2, "Fred"); statement.addBatch();//把這條執行語句加到 PreparedStatement 對象的批處理命令中

statement.addBatch(); //記錄3
statement.setInt(1, 3);
statement.setString(2, "Mark");
statement.addBatch(); //批量執行上面3條語句.
int [] counts = statement.executeBatch(); //Commit it 到(DB)裏面
**已安裝了microsoft 驅動程序jdk1.4; win2000 server; MS sql2000;*/import java.sql.*;
class sql
{public static void main(String[] agrs)
{
Connection cn=null;
Statement stmt=null;
String sql=null; try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println("Not find the Driver!");
}try
{
String urls="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webroot";//webroot 庫名.TALBE 是表名;
String user="sa";
String password="password";
cn= DriverManager.getConnection(urls,user,password);//stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);sql="select top 10 * from TABLE1";
ResultSet rs= stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString(2)+" "+rs.getString(3));}rs.first();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 1 "+rs.getString(3));rs.last();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 2 "+rs.getString(3));rs.previous();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 3 "+rs.getString(3));rs.next();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 4 "+rs.getString(3));rs.absolute(2);
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 5 "+rs.getString(3));/*
rs.afterLast();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" "+rs.getString(3));
System.out.print(rs.isAfterLast());rs.beforeFirst();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" "+rs.getString(3));
*/String sql1="update TABLE1 set 題目=? where id=? ";
PreparedStatement stmt1 = cn.prepareStatement(sql1);
String stat = new String("盛夏話足部保健");
String stat1 = UnicodeToGB(stat);//解決中文問題
stmt1.setString(1,stat1);
stmt1.setInt(2,3423);
stmt1.executeUpdate();
cn.commit();//System.out.println(stat1);
//System.exit(0);//cn.setAutoCommit(false);stmt.addBatch("update TABLE1 set 題目="盛夏話足部保健1" where id="3407"");
stmt.addBatch("update TABLE1 set 題目="夏季預防中暑膳食1" where id="3408"");
stmt.addBatch("INSERT INTO TABLE1 VALUES("11","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("12","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("13","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("14","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("15","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("16","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("17","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("18","12","13","","")");int [] updateCounts=stmt.executeBatch();
cn.commit();stmt.close();
cn.close();
}
catch(SQLException e)
{
System.out.println("The SQLException error!");
}}
/*
*/public static String UnicodeToGB(String strIn){
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}
catch(Exception e){
System.out.println("unicodeToGB exception : " + e.getMessage() + "\n");
}
return strOut;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章