您好,我正在制作一个应用程序,用于将数据从文本文件插入数据库。现在,我的代码正确地插入了信息,但是当数据字段中出现错误时,程序将停止并且不插入其余信息。它还使用log4j记录错误,但不会继续执行。在读取错误后,如何记录错误并继续运行呢?到目前为止,这就是我所拥有的。我很确定我必须在某个地方放一个循环,但是当我这样做时,它会给我一个错误或发生我不想要的事情。
System.out.println("create jdbc connection using properties file");
Connection con = null;
// use try becuase it tends to fail sometimes and we want error messages
try {
//load properties file we have created
Properties prop = loadPropertiesFile();
//declare vars and get the properties at the same time
String driverClass = prop.getProperty("SQLSERVERJDBC.driver");
// have to instance driver
Class.forName(driverClass).newInstance();
// make connection object using the previous things as parameters
try{
con = DriverManager.getConnection("jdbc:sqlserver://"+txtuserinputhost.getText()+":"+txtuserinputport.getText()+";DatabaseName=FRONTMC", txtuserinputuser.getText(), txtuserinputpassword.getText());
}catch(SQLException errorid){
logger.error("ERROR CREDENCIALES");
new Thread(new Runnable() {
@Override
public void run() {
errorcon();
}
}
).start();
}
//this if is to verify the connection
if (con != null) {
System.out.println("connection created successfully using properties file");
BufferedReader reader = new BufferedReader(new FileReader(
"C:\\Users\\darroyo\\Documents\\pruebasx.txt"));
ArrayList<String> array1 = new ArrayList<String>();
// Read line from file.
try{
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
// Split line on space.
String[] parts = line.split("");
//part in parts
for (String part : parts) {
//part is the element in this case each fix tag with value
array1.add(part);
}
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))";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = con.prepareStatement(query);
for(int counter =0; counter< array1.size();counter++){
if(array1.get(counter).substring(0, 3).equals("37=")){
preparedStmt.setString (1, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 3).equals("37=")){
preparedStmt.setString (2, array1.get(counter).substring(3));
}
// change emisora and serie to 48 with bd
if(array1.get(counter).substring(0, 3).equals("49=")){
preparedStmt.setString (3, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 4).equals("447=")){
preparedStmt.setString (4, array1.get(counter).substring(4));
}
if(array1.get(counter).substring(0, 3).equals("54=")){
preparedStmt.setString (5, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 3).equals("32=")){
preparedStmt.setString (6, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 3).equals("31=")){
preparedStmt.setString (7, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 4).equals("381=")){
preparedStmt.setString (8, array1.get(counter).substring(4));
}
if(array1.get(counter).substring(0, 3).equals("63=")){
preparedStmt.setString (9, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 4).equals("448=")){
preparedStmt.setString (10, array1.get(counter).substring(4));
}
if(array1.get(counter).substring(0, 4).equals("150=")){
preparedStmt.setString (11, array1.get(counter).substring(4));
}
if(array1.get(counter).substring(0, 3).equals("48=")){
preparedStmt.setString (12, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 2).equals("1=")){
preparedStmt.setString (13, array1.get(counter).substring(2));
}
if(array1.get(counter).substring(0, 4).equals("527=")){
preparedStmt.setString (14, array1.get(counter).substring(4));
}
if(array1.get(counter).substring(0, 3).equals("17=")){
preparedStmt.setString (15, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 3).equals("11=")){
preparedStmt.setString (16, array1.get(counter).substring(3));
}
if(array1.get(counter).substring(0, 3).equals("52=")){
String date = array1.get(counter).substring(3);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
String ds2 = sdf2.format(sdf1.parse(date));
String x = date.substring(9, 21);
String newfecha1 = ds2+" "+x;
preparedStmt.setString (17, newfecha1);
}
if(array1.get(counter).substring(0, 3).equals("52=")){
String date = array1.get(counter).substring(3);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
String ds2 = sdf2.format(sdf1.parse(date));
String x = date.substring(9, 21);
String newfecha1 = ds2+" "+x;
preparedStmt.setString (18, newfecha1);
}
}
// execute the preparedstatement
try{
preparedStmt.execute();
}catch(SQLException inserterr){
inserterr.printStackTrace();
}
}
System.out.println(array1);
reader.close();
}catch(Exception tryerror){
System.out.println("Error en dato");
}
//creating the statement(should check to use prepared statement in the future
// fecha recp y fecha sentra in query and for and ? and order
//notifies you that it was completed
System.out.println("insert complete");
//Read number of lines
File file = new File("C:\\Users\\darroyo\\Documents\\pruebasx.txt");
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
lineNumberReader.close();
countermsjinsertados.setText(Integer.toString(lines));
new Thread(new Runnable() {
@Override
public void run() {
exitomsj();
}
}
).start();
}
else {
System.out.println(" unable to create connection");
}
// loop to check the digits ex. for() array.1get(counter).substring(0,3).equals("23=")
//error messages
//ERROR CREDENCIALES
}catch (SQLException eb) {
System.out.println("error");
logger.error("ERROR DATOS");
new Thread(new Runnable() {
@Override
public void run() {
errorcon2();
}
}
).start();
}
//ERROR MESSAGE FECHA just get line number
catch (Exception eb) {
eb.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
答案 0 :(得分:0)
您想要的伪代码结构是:
while(there is more data to process) {
try {
do the work, maybe throw an exception
} catch (exceptions) {
log the error
if the exception is fatal, rethrow
}
}
try-catch
块处理在处理项目时发生的异常。
在catch
块中,如果您确定异常严重到足以取消整个过程(例如,数据库连接失败,使其无法继续执行),则将引发一个新异常,将原始异常包裹起来。由于您不在try
之外,因此此新异常将冒泡到封闭的上下文,您可以在那里处理它或让它终止该过程。
如果在可以继续处理其他项的意义上说,该异常是“可恢复的”,则上面的代码结构将完全做到这一点。
答案 1 :(得分:-1)
据我了解,无论您是否引发异常,您都希望程序执行某些操作。
在这种情况下,请尝试将代码放在finally块中,正是这样。
如果这不是您想要做的,请告诉我。