- import java.sql.*;
- public class AddRecord {
- public static int COUNT = 1000000;
- public static int THREAD_NUM = 100;
- public static String sql = "INSERT table1(id,password) VALUES(?,?)";
- public static void main(String[] args) {
- int start = 0;//每个线程开始的位置
- int count = COUNT / THREAD_NUM;//每个线程分配的任务量
- for(int i = 0; i < THREAD_NUM; i++) {
- String threadName = "{ " + i + " }";//线程名
- System.out.println("Start Thread : " + threadName);
- new AddThread(threadName, start, count).start();
- start +=count;
- }
- }
- }
- class AddThread extends Thread {
- private int start,count;
- public AddThread(String name, int start, int count) {
- super(name);
- this.start = start;
- this.count = count;
- }
- public void run() {
- try {
- int finished = 1;
- Class.forName("org.gjt.mm.mysql.Driver").newInstance();
- Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?" +
- "user=root&password=123456&useUnicode=true");
- PreparedStatement ps = con.prepareStatement(AddRecord.sql);
-
- for (int i = start; i < count; i++) {
- String pw = "password"+i;
- ps.setInt(1, i);
- ps.setString(2,pw);
- ps.execute();
-
- if(i >= (start+count*finished/100)) {
- System.out.println("Thread : " + getName() + "complete " + finished + "%");
- finished++;
- }
- }
- } catch (Exception e) {
- System.err.println("Thread : " + getName() + "error!exit");
- }
- }
- }
复制代码 |
|