对于以下代码,我在循环中插入2400多个行。在完成所有操作之前,我永远不会关闭连接。它引发异常。有什么问题,我在这里错过了什么?
private PreparedStatement pst = connection.prepareStatement(INSERT_FAILED_INTERIOR_ROOM);
private Connection connection = null;
public static void insertIntoFaildIRoom(String master, String room, String xmlContent) {
try {
pst.setString(1, master);
pst.setString(2, room);
pst.setString(3, xmlContent);
pst.executeUpdate();
connection.commit();
System.out.println("failed room insert commited");
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException e1) {
/* Nothing */}
String errorMsg = String.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
System.out.println(errorMsg);
} catch (Exception ex) {
System.out.println("Error when insert in failed room " + ex.getMessage());
}
}
public String addResponse(){
try {
if (connection == null) {
connection = MainConnection.open();
}
if (connection != null) {
System.out.println("Connected to the database!");
InteriorRoomBatch batch = new InteriorRoomBatch();
batch.fillInteriorDetailsFromCache();
for (InteriorDetails interiorDetails : batch.getInteriorDetailsList()) {
addRoomQuery.insertIntoFaildIRoom(interiorDetails.getMaster(), interiorDetails.getRoom(), interiorDetails.xmlContent() );
}
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
// After inserting coupel of hundrands of row, it throw excetion ORA-01000: maximum open cursors exceeded
String errorMsg = String.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
System.out.println(errorMsg);
}finally {
close(pst);
connection.close();
System.out.println("close the PreparedStatement after");
}
return "ok";
}