批量执行sql

来源:互联网 发布:理发店发型设计软件 编辑:程序博客网 时间:2024/05/06 07:30
public static int batchImportSql(String sql){
Connection con = null;
PreparedStatement ps = null;
Statement statement = null;
ResultSet rs = null;
int i = 0;
try {
String[] sqlTem=sql.split(";");//sql语句,以分号分开
con = ds.getConnection();
con.setAutoCommit(false);
statement = con.createStatement();    
/*
*将传进来的sql进行拆分
*/
for(int j=0;j<sqlTem.length;j++){
statement.addBatch(sqlTem[j]);
}
statement.executeBatch(); 
con.commit();
} catch (SQLException e) {
i=-1;
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally
{
close(rs, ps, con);
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}