少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
1、Connection创建类

package com.abin.lee.db.oracle.batch;

import java.sql.Connection;
import java.sql.DriverManager;

public class OracleConnection {
 private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
 private static final String URL="jdbc:oracle:thin:@localhost:1521:XE";
 private static final String USER="abin";
 private static final String PWD="abin";
 private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
 
 public static Connection getConnection(){
  Connection conn=null;
  if(null==threadLocal.get()){
   try {
    Class.forName(DRIVER);
    conn=DriverManager.getConnection(URL, USER, PWD);
    threadLocal.set(conn);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }else{
   conn=threadLocal.get();
  }
  return conn;
 }
 
 
}






2、测试语句

package com.abin.lee.db.oracle.batch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import junit.framework.TestCase;

public class OracleBatch extends TestCase {
 public void testinsert() throws SQLException {
  Connection conn = null;
  PreparedStatement ps = null;
  conn = OracleConnection.getConnection();
  String sql = "insert into abing(id,name) values (?,?)";
  ps = conn.prepareStatement(sql);
  System.out.println("start="
    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
      .format(new java.util.Date()));
  try {
   for (int i = 0; i <= 50000; i++) {
    ps.setObject(1, i);
    ps.setObject(2, "abin" + i);
    ps.addBatch();
    if (i % 10000 == 0) {
     // System.out.println("i="+i);
     ps.executeBatch();
     ps.clearBatch();
    }

   }

  } catch (SQLException e) {
   e.printStackTrace();
  }
  System.out.println("end="
    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
      .format(new java.util.Date()));

 }

}

posted on 2012-09-17 23:15 abin 阅读(359) 评论(0)  编辑  收藏 所属分类: oracle

只有注册用户登录后才能发表评论。


网站导航: