import java.text.SimpleDateFormat;
import java.util.Date;
public static void addDate() {
// 获取数据库连接
Connection conn = DBUtil.getConnection();
// 创建preparedstatement
PreparedStatement pmst = null;
// 创建resultSet【在这里他没啥用的增删改不需要它】
ResultSet re = null;
try {
// 设置手动提交数据
conn.setAutoCommit(false);
String sql = "insert into classinfo(cid,cname) values(?,?)";
pmst = conn.prepareStatement(sql);
for (int i = 1; i < 1000000; i++) {
pmst.setString(1, i + "");
pmst.setString(2, "wangwang");
pmst.addBatch();
}
// 返回一个int数组
int[] count = pmst.executeBatch();
if (count.length > 0) {
System.out.println("批量操作了" + count.length + "条数据。。。。。。。。。");
}
conn.commit();// 提交数据
} catch (Exception e) {
e.printStackTrace();
try {
// 操作失败回滚数据
conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
} finally {
// 这个是用来关闭connection preparstatement resultset 方法自己写去
DBUtil.closeConnection(conn, pmst, re);
}
}
public String saveDeviceItem(List<String> list) {
// TODO Auto-generated method stub
//通过循环将list中的SQL语句放到同一个事务中提交
Connection conn=ConnectionFactory.GetConnection();
Statement st=null;
try {
conn.setAutoCommit(false);
st=conn.createStatement();
if(list!=null && list.size()>0){
for(int i=0;i<list.size();i++){
if(list.get(i)!=null){
st.addBatch(list.get(i));
}
}
}
int count[]=st.executeBatch();
conn.commit();
if(count.length>0){
return "SUCCESS";
}
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e2) {
e2.printStackTrace();
}
}finally{
try {
st.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
ConnectionFactory.close(conn);
}
return "ERRORNUM005";
}
JDBC批量操作数据
最新推荐文章于 2022-12-20 20:18:31 发布