批处理 你插入一条 就连接一次数据库。。。。几万条 就连接几万次 理论和实际效率肯定会相对低一点呢 你可以吧你的数据装一个LIST里面 一次插入1000条 用毫秒数试试要多久 会不会好点 10000条又多久。。(当然 看你数据有多大的 不大就没问题) 然后循环插就好了 还有数据库表主键索引都建一下
请搜索SQL.addBatch()方法,插入3万条,只需要几秒
public void fast_insert(PreparedStatement pst) {
try {
synchronized (pst) {
ct.setAutoCommit(false);
pst.executeBatch();
ct.commit();
}
定义一个PST。写在类里
PreparedStatement pst1 = null;
try {
pst1 = (PreparedStatement) sql.getConnection().prepareStatement(
"insert into cangku (c_number,c_list,c_phone,c_out) values(?,?,?,?)");
} catch (SQLException e1) {
e1.printStackTrace();
}
int ab = 1;
for (int h = 0; h < rows; h++) {
String number1 = rs.getCell(0, h).getContents().toString();
if (number1.length() < 2 || map.get(number1) != null) {
continue;
}
String list1 = rs.getCell(1, h).getContents().toString();
String phone1 = rs.getCell(2, h).getContents().toString();
String out1 = rs.getCell(5, h).getContents().toString();
try {
pst1.setString(1, number1);
pst1.setString(2, list1);
pst1.setString(3, phone1);
pst1.setString(4, out1);
pst1.addBatch();
} catch (SQLException e) {
e.printStackTrace();
}
p3_lab2.setText("插入了" + (ab++) + "条数据");
}
sql.fast_insert(pst1);//调用fast_insert方法,传入pst执行。
pst1 = null;
if(ab==1)
{
p3_lab2.setText("数据库资料已是最新!");
}
} catch (BiffException | IOException e1) {
e1.printStackTrace();
}