import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class InsertDataToMysql {
public static Connection conn = null;
public static PreparedStatement pst = null;
private static final String DBDRIVER = "com.mysql.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=UTF-8";
private static final String DBUSER = "root";
private static final String DBPASSWORD = "root";
public static void main(String[] args) {
Dao_mysql();
Test();
}
public static void Dao_mysql() {
try {
Class.forName(DBDRIVER);
// 使用DriverManager类的getConnection()方法建立连接
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
System.out.println("-------数据库连接成功 -------");
} catch (Exception e) {
e.printStackTrace();
System.out.println("------数据库连接失败------");
}
}
public static void Test(){
try {
String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
pst = conn.prepareStatement(sql);
conn.setAutoCommit(false);
Long startTime = System.currentTimeMillis();
Random rand = new Random();
int a,b,c,d;
for (int i = 1; i <= 100000; i++) {
pst.setInt(1, i);
pst.setInt(2, i);
a = rand.nextInt(10);
b = rand.nextInt(10);
c = rand.nextInt(10);
d = rand.nextInt(10);
pst.setString(3, "188"+a+"88"+b+c+"66"+d);
pst.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
pst.addBatch();
if(i%1000==0){
pst.executeBatch();
pst.clearBatch();
}
}
pst.executeBatch();
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("OK,用时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class InsertDataToMysql {
public static Connection conn = null;
public static PreparedStatement pst = null;
private static final String DBDRIVER = "com.mysql.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=UTF-8";
private static final String DBUSER = "root";
private static final String DBPASSWORD = "root";
public static void main(String[] args) {
Dao_mysql();
Test();
}
public static void Dao_mysql() {
try {
Class.forName(DBDRIVER);
// 使用DriverManager类的getConnection()方法建立连接
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
System.out.println("-------数据库连接成功 -------");
} catch (Exception e) {
e.printStackTrace();
System.out.println("------数据库连接失败------");
}
}
public static void Test(){
try {
String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
pst = conn.prepareStatement(sql);
conn.setAutoCommit(false);
Long startTime = System.currentTimeMillis();
Random rand = new Random();
int a,b,c,d;
for (int i = 1; i <= 100000; i++) {
pst.setInt(1, i);
pst.setInt(2, i);
a = rand.nextInt(10);
b = rand.nextInt(10);
c = rand.nextInt(10);
d = rand.nextInt(10);
pst.setString(3, "188"+a+"88"+b+c+"66"+d);
pst.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
pst.addBatch();
if(i%1000==0){
pst.executeBatch();
pst.clearBatch();
}
}
pst.executeBatch();
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("OK,用时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}