数据库表中批量插入数据
上一篇 /
下一篇 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{
}
}
}
相关阅读:
- oralce中的rownum的用法 (youzaiyizu, 2012-2-02)
- 利用SQL注入漏洞登录后台 (xin_晴, 2012-2-10)
- SQL Server全文索引的硬伤 (xin_晴, 2012-2-15)
- SQL Server数据库分页存储过程优化效率分析 (xin_晴, 2012-2-16)
- 关于SQL Server隐式转换 (xin_晴, 2012-2-22)
- 数据库学习笔记(一)2012-02-23 (SonofGod, 2012-2-23)
- 你了解SQL的索引原理吗 (xin_晴, 2012-2-28)
- 函数使得索引列失效 (xin_晴, 2012-3-06)
- 如何用SQL Server将数据库横向扩展 (xin_晴, 2012-3-08)
- SQL Server数据库用视图来处理复杂的数据查询关系 (xin_晴, 2012-3-15)
收藏
举报
TAG:
SQL
sql