下面代码的问题是,假设如果查询 [] 数组中的特定查询包含错误,则语句查询将执行,并且将引发有错误的查询的异常,我希望两个查询都执行查询没有错误,否则不应该执行任何查询,我应该如何处理下面的代码以获得所需的结果?请帮忙。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int rw = jTable1.getRowCount();
int col = jTable1.getColumnCount();
System.out.println("RC" + rw);
System.out.println("Col" + col);
String queries[] = new String[rw];
Object o[][] = new Object[rw][col];
String poid = jComboBox10.getSelectedItem().toString();
java.sql.Date xdate = null;
Connection con=null;
Statement st=null;
String cc = ims.MainWindow.cc;
try {
for (int i = 0; i < rw; i++) {
for (int j = 0; j < col; j++) {
o[i][j] = jTable1.getValueAt(i, j);
System.out.println("imj" + i + "," + j + "=" + o[i][j]);
}
if (String.valueOf(o[i][5]) == "") {
xdate=null;
}
else {
xdate = new java.sql.Date(df.parse(String.valueOf(o[i][5])).getTime());
}
queries[i] = "insert into po_items values('" + poid + "','" + cc + "','" + o[i][1] + "'," + o[i][2] + "," + o[i][4] + ",'" + xdate + "','" + o[i][7] + "'," + o[i][8] + ")";
}
for (int k = 0; k < rw; k++) {
System.out.println(queries[k]);
}
String query = "insert into tablex values('xx','yy')";
con=CPool.getConnection();
st=con.createStatement();
con.setAutoCommit(false);
for(int l=0;l<rw;l++)
{
st.addBatch(queries[l]);
}
st.addBatch(query);
st.executeBatch();
con.commit();
System.out.println(query);
} catch (Exception x) {
System.out.println(x.getMessage());
}
finally {
CPool.closeConnection(con);
CPool.closeStatement(st);
}
}