1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 在执行批处理时将执行错误的语句记录下来并写到日志文件中去

在执行批处理时将执行错误的语句记录下来并写到日志文件中去

时间:2020-11-19 03:04:39

相关推荐

在执行批处理时将执行错误的语句记录下来并写到日志文件中去



public static void executesqls(List<String> sqlList) throws IOException{//改

Connection con = null;

Statement sm = null;

File file = new File("d:\\a.txt");//此处不能写死,后续要改

FileWriter writer = new FileWriter(file, true);

String outPutFile="";

String endLine = System.getProperty("line.separator"); // 获取换行符

try {

// 加载驱动

Class.forName("oracle.jdbc.driver.OracleDriver");

// 得到连接

con = DriverManager.getConnection(

"jdbc:oracle:thin:@192.168.1.0:1521:orcl", "username", "password");

con.setAutoCommit(false);// 关闭自动提交,提高执行效率

sm = con.createStatement();

for (int i = 0; i < sqlList.size(); i++) {

sm.addBatch(sqlList.get(i));

if (i % 1000 == 0 || i == (sqlList.size() - 1)) {// 每1000条提交一次,避免内存溢出,应每x条提交一次数据。

/*sm.executeBatch();

mit();

sm.clearBatch();*/

}

}

sm.executeBatch();

mit();

sm.clearBatch();

} catch (Exception e) {

if (e instanceof BatchUpdateException) {

BatchUpdateException bException = (BatchUpdateException) e;

int[] s = bException.getUpdateCounts();

outPutFile = "语句: " + sqlList.get(s.length) + " 执行失败" + endLine;

writer.write(outPutFile);// 将出错语句写入到文件

writer.flush();

if (s.length + 1 < sqlList.size()) {

List<String> sList = sqlList.subList(s.length + 1, sqlList.size());

executesqls(sList);

}

} else {

e.printStackTrace();

try {

if (sm != null)

sm.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

try {

con.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

}

} finally {

try {

if (sm != null)

sm.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

try {

con.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

if (writer != null) {

writer.close();

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。