java - 发现异常后继续执行
问题描述
我正在制作一个小程序来从文本文档插入数据库。我使用哈希图从文本文档中获取值,因为我不想使用数组并且文本文档的格式为 Key:value。目前我的代码插入正确,但是当发现异常时,程序在异常点结束。我的目标是即使在发现异常后也让它运行,它不一定会忽略它,但我想继续执行。
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
"C:\\Users\\darroyo\\Documents\\pruebasx.txt"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
String line;
try {
line = reader.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
String query = " insert into FRONTMC.HECHO (folio_hecho, folio_orden, emisora, serie,"
+ "clave_sentido, titulos_hecho, precio, importe, liquidacion, contraparte, id_estatus, isin, contrato,"
+ "secondary_exec_id, exec_id, F11_ClOrdID, fecha_recepcion, fecha_sentra)"
+ " values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,convert(varchar(30),cast(? as datetime),120),convert(varchar(30),cast(? as datetime),120))";
PreparedStatement preparedStmt = null;
try {
preparedStmt = con.prepareStatement(query);
} catch (SQLException e1) {
e1.printStackTrace();
}
Map<Integer,String> hm1 = new HashMap<Integer,String>();
try {
while ((line = reader.readLine()) != null) {
String[] tokens = line.split("");
for (int i = 0; i != tokens.length; i++) {
int dataIndex = tokens[i].indexOf('=') + 1;
String data = tokens[i].substring(dataIndex);
hm1.put(new Integer(i),data);
}
for(int counter =0;counter <=line.length();counter++){
preparedStmt.setString(1, hm1.get(19));
preparedStmt.setString(2, hm1.get(19));
preparedStmt.setString(3, hm1.get(3));
preparedStmt.setString(4, hm1.get(34));
preparedStmt.setString(5, hm1.get(15));
preparedStmt.setString(6, hm1.get(30));
preparedStmt.setString(7, hm1.get(16));
preparedStmt.setString(8, hm1.get(18));
preparedStmt.setString(9, hm1.get(8));
preparedStmt.setString(10, hm1.get(33));
preparedStmt.setString(11, hm1.get(27));
preparedStmt.setString(12, hm1.get(17));
preparedStmt.setString(13, hm1.get(26));
preparedStmt.setString(14, hm1.get(23));
preparedStmt.setString(15, hm1.get(10));
preparedStmt.setString(16, hm1.get(14));
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
String ds2 = sdf2.format(sdf1.parse(hm1.get(6)));
String newfecha1 = ds2;
preparedStmt.setString(17, newfecha1);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf4 = new SimpleDateFormat("dd/MM/yyyy");
String ds4 = sdf4.format(sdf3.parse(hm1.get(6)));
String newfecha3 = ds4;
preparedStmt.setString(18, newfecha3);
}
try{
preparedStmt.execute();
}catch(SQLException e3){
e3.printStackTrace();
reader.readLine();
continue;
}
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
System.out.println("e1.getmessage()");
}
我曾尝试将准备好的语句包含在 try catch 中,但它仍然停止,正如您所看到的,我也使用了 continue,但它仍然停止。一切运行正常,我只想在出错后继续执行,问题出在哪里?目前的错误是 ParseException 底部的错误。我想在得到这个异常后运行。
解决方案
您需要将try/catch
块移动到while
:
while ((line = reader.readLine()) != null) {
try {
String[] tokens = line.split("");
for (int i = 0; i != tokens.length; i++) {
int dataIndex = tokens[i].indexOf('=') + 1;
String data = tokens[i].substring(dataIndex);
hm1.put(new Integer(i),data);
}
// Try to parse line and insert into db
preparedStmt.setString(1, hm1.get(19));
preparedStmt.setString(2, hm1.get(19));
// etc....
// No other try/catch here to skip entire
// line on error
preparedStmt.execute();
}
catch (SQLException e1) {
e1.printStackTrace();
}
catch (IOException e2) {
e2.printStackTrace();
}
catch (ParseException e3) {
System.out.println("e3.getmessage()");
}
} // while
推荐阅读
- python - 用于返回最新并使用不同列的 flask-sqlalchemy 查询
- python - 问题循环回到程序的开始
- karate - 功能文件中的空手道强制场景在多个线程上顺序执行
- node.js - NodeJS SQL Server 数据拉取不等待承诺完成
- javascript - 有没有办法搜索所有具有特定值的 Javascripts 变量?
- javascript - React 功能组件:一旦达到窗口断点,尝试设置状态
- kubernetes - Kubernetes - 用户“system:serviceaccount:management:gitlab-admin”无法在命名空间“services”中的 API >group“”中获取资源“serviceaccounts”
- html - 为原始文本中的 \n 换行设置自定义高度
- autodesk-data-management - 如何找到 BIM360 Docs 文件的 Bucketkey?
- python - 为在谷歌云应用引擎上运行的 python 运行 PlayWright