批处理导入数据,不怕麻烦可以把对象换成map也是可以的
/**
* 批量导入list
* @param list 需要导入的list
* @param key 添加的属性,顺序需要跟insert into 表名后接的字段名相同
* @param sql 添加的sql语句
* @throws SQLException
* @throws IOException
*/
public static void batchAddData(List<Map<String, String>> list, String key,
String sql) throws SQLException, IOException {
Connection conn = new StartBatchUtil().getConnection();
if (conn == null) {
return;
}
PreparedStatement pst = conn.prepareStatement(sql);
String[] keys = key.split(",");
for (int i = 0; i < list.size(); i++) {
String[] values=new String[keys.length];
for (int j = 1; j <= keys.length; j++) {
pst.setString(j, list.get(i).get(keys[j - 1].trim()));
values[j-1]=list.get(i).get(keys[j - 1].trim());
}
log.error(PrintCompleteSql.getSql(values, sql));
pst.addBatch();
}
try {
pst.executeBatch();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
log.error(e.getMessage());
}
}
}