pstmt=con.prepareStatement("insert into UserLogin values(?,?)");
pstmt.setString(1, "haha4");
pstmt.setString(2, "haha4");
pstmt.addBatch();
pstmt.setString(1, "haha5");
pstmt.setString(2, "haha5");
pstmt.addBatch();
pstmt.setString(1, "haha6");
pstmt.setString(2, "haha6");
pstmt.addBatch();
pstmt.executeBatch();
pstmt.close();
con.close();程序在pstmt.executeBatch();的地方就一直不动了。
表里面大约5多万条数据,当然这里原始的循环插入数据,大约近万条,不过每一千条就提交一次。。
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;import test.mysql.MConnection;public class UUIDTest {
public static void test() {
Connection con = null;
PreparedStatement ps = null;
try {
con = MConnection.getConn();
String sql = "insert into uuidtest(uuid) values(?)";
ps = con.prepareStatement(sql);
for (int i = 0; i < 10000; i++) {
String uuid = UUID.randomUUID().toString();
System.out.println(uuid);
ps.setString(1, uuid);
ps.addBatch();
}
ps.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
UUIDTest.test(); }
}
如果autoCommit是false,当然要等你提交。