数据库表中批量插入数据

上一篇 / 下一篇  2012-03-16 16:28:30 / 个人分类:sql

下面程序用的java工程,往mysql数据库ppUser表中批量插入五万条记录。在测试的时候需要导入jar包(mysql-connector-java-5.1.13.jar)

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


public class HttpClient {
 public static void main(String argv[]) throws Exception {
  
  insertData();
  //updateData();
 }
 
 
 private static void insertData() {
  // TODO Auto-generated method stub
 
  String url="jdbc:mysql://hub2.paopaoyun.com:3306/paopaoyun";
  String user="tester";
  String password="resworb";
  String str="";
  String s = null;
  
  try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   Connection con = (Connection) DriverManager.getConnection(url,user,password);
   
   //插入ppUser表
   con.setAutoCommit(false);
   PreparedStatement pst3 = (PreparedStatement) con.prepareStatement("INSERT INTO `paopaoyun`.`ppUser` (`id` ,`creationDate` ,`email` ,`encryptedPassword` ,`modificationDate` ,`name` ,`plainPassword` ,`username` ,`emailVerifyed` ,`keyId`)VALUES (?,? , ?, ?, ?, ?, ?, ?, ?, ?) ");
   for(int i = 1;i<50001;i++){
    System.out.println("abc"+i);
    pst3.setLong(1, i+10000);//根据实际情况拼写ID
    pst3.setString(2, "2012-01-29 0:00:00");//creationDate
    pst3.setString(3, "test@kortide.com.cn");//email
    pst3.setString(4, s);//encrtptedPassword
    pst3.setString(5, s);//modificationDate
    pst3.setString(6, "");//name
    pst3.setString(7, "123456");//plainPassword
    str = "" + i;
    pst3.setString(8, str);//username
    pst3.setInt(9, 1);//emailVerifyed
    pst3.setString(10, "");//keyId
   
    pst3.addBatch();
    
   }
   pst3.executeBatch();
   con.commit();
   pst3.close();
   con.close();
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   
  }  
 }  
}


TAG: SQL sql

lvxinsi的个人空间 引用 删除 lvxinsi   /   2012-03-19 09:57:32
实际工作中用存储过程是不是效率更高呢?
 

评分:0

我来说两句

日历

« 2024-04-28  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 15959
  • 日志数: 18
  • 建立时间: 2012-01-30
  • 更新时间: 2012-06-07

RSS订阅

Open Toolbar