batch data update
public class BatchData {
public static void main(String[] args) {
try {
testBatchData();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void testBatchData() throws SQLException, ClassNotFoundException{
String sql1 = "insert into user (name,address) values ('name111','address111')";
Connection conn = JdbcMySqlUtil.getInstance().getConnection() ;
PreparedStatement ps = conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
int id = 0 ;
if (rs.next())
id = rs.getInt(1);
System.out.print(id);
String sql2 = "insert into user (name,address) values ('name111','address111')";
PreparedStatement ps2 = conn.prepareStatement(sql2);
for (int i = 0 ; i < 10 ; i ++)
ps2.addBatch(sql2);
ps2.executeBatch();
JdbcMySqlUtil.free(rs, ps2, ps2.getConnection());
}
}