package com.jdbctest.batch;
import com.jdbctest.Util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//PreparedStatement接口的批量更新
public class PreparedStatementBatchtest {
public static void main(String args[]){
Connection conn = null;
PreparedStatement ps = null;
conn = JDBCUtil.getConnection();
String sql="INSERT INTO score(sno,sname,score)"+"VALUES(?,?,?)";
try {
long start =System.currentTimeMillis();
ps = conn.prepareStatement(sql);
for(int i = 0 ; i<10000;i++){
ps.setString(1, "20060800"+i);
ps.setString(2, "wuyu");
ps.setString(4, 88);
ps.addBatch();
}
ps.executeBatch();
long end=System.currentTimeMillis();
System.out.println("用时: "+(end-start));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.jdbctest.batch;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.jdbctest.Util.JDBCUtil;
/**
* Statement 批量更新
* @author wuyu
*
*/
public class StatementBatchTest {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
conn = JDBCUtil.getConnection();
try {long begin=System.currentTimeMillis();
stmt = conn.createStatement();
for(int i = 1; i<10000;i++){
stmt.addBatch("INSERT INTO score(sno,sname,score)"+"VALUES('20006080"+i+"','wuyu',88.9)");
}
stmt.executeBatch();
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-begin));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
批量更新(batch)
最新推荐文章于 2023-11-08 16:11:12 发布