java中SQL的批处理

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;


import org.junit.Test;


import com.itheima.utils.DBCPUtil;


public class Test02 {
Connection conn = null;
Statement stmt = null;
@Test
public void execBatchSql() {
 
conn = DBCPUtil.getConnection();
 
 try {
stmt = conn.createStatement();
String sql1 = "insert into ccc values(11,'k11')"; 
String sql2 = "insert into ccc values(12,'k12')";
String sql3 = "insert into ccc values(13,'k13')";
String sql4 = "delete from ccc where name='k10'";
 
 
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
 
int[] influNum =  stmt.executeBatch();  //返回每行受影响的语句条数

for (int i : influNum) {
System.err.println(i);
}

       }catch(Exception e) {
        throw new RuntimeException(e);
       }finally {
        DBCPUtil.release(null, stmt, conn);
       }
 
 
}
 @Test
public void execBatchSql01() {
 
conn = DBCPUtil.getConnection();
PreparedStatement  stmt = null;
 try {
stmt = conn.prepareStatement("insert into ccc values(?,?)"); 
 
for(int i = 15 ;i<20;i++) {
stmt.setInt(1,i );
stmt.setString(2, "k"+i);
stmt.addBatch();
 
// stmt.clearBatch()  清空batch中的SQL语句
}
int[] influNum =  stmt.executeBatch();  //返回每行受影响的语句条数

for (int i : influNum) {
System.err.println(i);
}
       }catch(Exception e) {
        throw new RuntimeException(e);
       }finally {
        DBCPUtil.release(null, stmt, conn);
       }


}

}

你可能感兴趣的:(java中SQL的批处理)