标签:
批处理是一次性向数据库发出多条查询指令,一起执行
Statement 接口定义的方法:
|—增加批处理语句:
public void addBatch(String sql)
|—执行批处理:
public int [] executeBatch()throws SQLException
PreparedStatement接口定义的方法:
增加批处理:public void addBatche()throws SQLexceeption
范例:
public class TestConn {
static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
static final String url="jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String USER="***";
private static final String PASSWORD="***";
public static void main(String[] args) throws Exception {
Class .forName(DBDRIVER);
Connection conn=DriverManager.getConnection(url,USER,PASSWORD);
Statement stmt=conn.createStatement();
conn.setAutoCommit(false);
try{//增加数据
stmt.addBatch("Insert into myemp(ename,job)values(‘sdvb1‘,‘测试ad‘)");
stmt.addBatch("Insert into myemp(ename,job)values(‘sdvb2‘,‘测试ad‘)");
stmt.addBatch("Insert into myemp(ename,job)values(‘sdvb3‘,‘测试ad‘)");
stmt.addBatch("Insert into myemp(ename,job)values(‘sdvb4‘,‘测试ad‘)");
int result[]=stmt.executeBatch();
System.out.println(Arrays.toString(result));
conn.commit();//没有错误,提交数据
}catch(Exception e){
e.printStackTrace();
conn.rollback();//有错误,回滚数据
}
conn.close();
}
}
标签:
原文地址:http://www.cnblogs.com/linkpauk/p/5934680.html