大家谁有实例:通过setExecuteBatch()方法和标准的executeUpdate()方法实现批量处理更新和插入。
如果成批地处理插入和更新操作,就能够显著地减少它们所需要的时间。Oracle提供的Statement和 CallableStatement并不真正地支持批处理,只有PreparedStatement对象才真正地支持批处理。我们可以使用addBatch()和executeBatch()方法选择标准的JDBC批处理,或者通过利用PreparedStatement对象的setExecuteBatch()方法和标准的executeUpdate()方法选择速度更快的Oracle专有的方法。要使用Oracle专有的批处理机制,可以以如下所示的方式调用setExecuteBatch():
PreparedStatement pstmt3D null;
try {
((OraclePreparedStatement)
pstmt).setExecuteBatch(30);
...
pstmt.executeUpdate();
}
如果成批地处理插入和更新操作,就能够显著地减少它们所需要的时间。Oracle提供的Statement和 CallableStatement并不真正地支持批处理,只有PreparedStatement对象才真正地支持批处理。我们可以使用addBatch()和executeBatch()方法选择标准的JDBC批处理,或者通过利用PreparedStatement对象的setExecuteBatch()方法和标准的executeUpdate()方法选择速度更快的Oracle专有的方法。要使用Oracle专有的批处理机制,可以以如下所示的方式调用setExecuteBatch():
PreparedStatement pstmt3D null;
try {
((OraclePreparedStatement)
pstmt).setExecuteBatch(30);
...
pstmt.executeUpdate();
}
import java.sql.PreparedStatement;
import java.sql.SQLException;import JdbcExp1.JdbcUtil;public class Lab3 {
public static void main(String[] args) {
Connection con=null;
PreparedStatement ps=null;
try {
con=JdbcUtil.getConnection();
con.setAutoCommit(false);
String sql="insert into vi_batch values(?,?)";
ps=con.prepareStatement(sql);
System.out.println(new java.util.Date());
for(int i=1;i<100001;i++){
ps.setInt(1, i);
ps.setString(2, "batch"+i);
//ps.executeUpdate();
ps.addBatch();
if(i%10000==0){
ps.executeBatch();
}
}
System.out.println(new java.util.Date());
con.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtil.release(null, ps, con);
}
}}
import java.sql.*;public class JdbcUtil
{
static{
try{
String driverName="oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection(){
String url="jdbc:oracle:thin:@10.30.19.211:1521:tarena";
String username="openlab";
String pwd="open123";
Connection con=null;
try{
con=DriverManager.getConnection(url,username,pwd);
}catch(Exception e){e.printStackTrace();}
return con;
}
public static void release(ResultSet rs,Statement stmt,Connection con){
try{if(rs!=null)rs.close();}catch(Exception e){e.printStackTrace();}
try{if(stmt!=null)stmt.close();}catch(Exception e){e.printStackTrace();}
try{if(con!=null)con.close();}catch(Exception e){e.printStackTrace();}
}
public static void useMetaData(ResultSet rs){
try {
if(rs==null){
System.out.println("rs is null");
return;
}
StringBuffer sb=new StringBuffer();
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();//得到字段个数
while(rs.next()){
for(int i=1;i<=cols;i++){
sb.append(md.getColumnName(i)+"="+rs.getString(i)+" ");
}
sb.append("\n");
}
System.out.println(sb);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean insert(Statement stmt,String sql){
try {
int i=stmt.executeUpdate(sql);
if(i>0)
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public static void release(Object o){
try{if(o instanceof ResultSet){
((ResultSet)o).close();
}else if(o instanceof Statement){
((Statement)o).close();
}else if(o instanceof Connection){
((Connection)o).close();
}}catch(Exception e){e.printStackTrace();}
}
}
以前的练习,你看一下吧
http://topic.csdn.net/u/20091023/11/ff523d58-c1e0-46c1-8cd2-9eed389e9265.html?73367
List<GameTypeVO> list = new ArrayList<GameTypeVO>();
conn = (Connection) db.getConn();
String sql = "select * from gametypetable group by gametypename order by gametypeid limit 0,5";
PreparedStatement pre = (PreparedStatement) conn.prepareStatement(sql);
ResultSet res = (ResultSet) pre.executeQuery();
while (res.next()) {
typevo = new GameTypeVO(res.getInt(1), res.getString(2));
list.add(typevo);
}
// System.out.println(list.size());
return list;
}