现在的位置: 首页 > 综合 > 正文

jdbc数据库性能测试

2013年07月10日 ⁄ 综合 ⁄ 共 2310字 ⁄ 字号 评论关闭

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

public class TestJdbcSpeed {
 
 public static void main(String[] args) {
  Connection conn = null;
  PreparedStatement ps = null;
  
//  String DB_DRIVER="ntong.jdbc.ntsql.NTSqlDriver";
//  String url = "jdbc:ntsql:192.168.1.128:9507";
  String DB_DRIVER= "oracle.jdbc.driver.OracleDriver";
  String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  String user = "test";
  String password = "test";
  
  String sql_drop = "drop table tb_test";
  String sql_create = "create table tb_test(id int)";
  String sql_delete = "delete from tb_test";
  String sql_insert = "insert into tb_test values(?)";
  
  
  try {
   Class.forName( DB_DRIVER );
   conn = DriverManager.getConnection(url, user, password);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  try {
   //conn.prepareStatement(sql_drop).executeUpdate();    //drop table
   //conn.prepareStatement(sql_create).executeUpdate();  //create table
   conn.prepareStatement(sql_delete).executeUpdate();  //delete recored
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  
  try {
   ps = conn.prepareStatement(sql_insert);
  } catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  
  
  long timestamp = 1000; //设置时间戳
  int count = 0; //计数器,也作临时序列使用
  long l2 ; //声明结束时间戳
  System.out.println("开始时间     :"+new TestJdbcSpeed().getCurrentTimeS());
  long l1 = System.currentTimeMillis(); //获取开始时间戳
  do  {
   count++;
   
   try {
    ps.setInt(1, count);
//    ps.executeUpdate();
    
    //oracle批处理
    ps.addBatch();
    if(count%50000==0){
     ps.executeBatch();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   
   l2 = System.currentTimeMillis();
  } while((l2-l1)<timestamp);
  System.out.println("结束时间     :"+new TestJdbcSpeed().getCurrentTimeS());
  System.out.println("实际时间戳:"+(l2-l1));
  System.out.println("循环次数     :"+count);
  
  
 }
 
 
 //获取当前系统时间
 public String getCurrentTimeS(){
  java.util.Date date = new java.util.Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");
  String str = sdf.format(date);
  if(str.length()==16){
   str = str.substring(0,14)+"0"+str.substring(14);
  }else if(str.length()==15){
   str = str.substring(0, 14)+"00"+str.substring(14);
  }else{
   //str = str+"";
  }
  return str;
 }

}

抱歉!评论已关闭.