public synchronized void create(Connection conn, MovementVO valueObject) throws SQLException { PreparedStatement stmt = null; try { String sql = "INSERT INTO TBL_MOVIMIENTO ( CODIGO, FECHA, CUENTA, VALOR, CONCEPTO, OBSERVACION) " + "VALUES (SEC_MOVIMIENTO.nextval, ?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(sql); stmt.setDate(1, Util.getSqlDate(valueObject.getDate())); stmt.setString(2, valueObject.getAccount().getId()); stmt.setObject(3, valueObject.getValue(), Types.DOUBLE); stmt.setString(4, valueObject.getConcept()); stmt.setString(5, valueObject.getObservation()); int rowcount = stmt.executeUpdate(); if (rowcount != 1) { throw new SQLException("PrimaryKey Error when updating DB!"); } conn.commit(); } finally { if (stmt != null) { stmt.close(); } } }
public void delete(Connection conn, MovementVO valueObject) throws SQLException { if (valueObject.getId() == null) { throw new SQLException("Can not delete without Primary-Key!"); } String sql = "DELETE FROM TBL_MOVIMIENTO WHERE (CODIGO = ? ) "; PreparedStatement stmt = null; try { stmt = conn.prepareStatement(sql); stmt.setObject(1, valueObject.getId(), Types.INTEGER); int rowcount = stmt.executeUpdate(); if (rowcount == 0) { throw new SQLException("Object could not be deleted! (PrimaryKey not found)"); } if (rowcount > 1) { throw new SQLException("PrimaryKey Error when updating DB! (Many objects were deleted!)"); } conn.commit(); } finally { if (stmt != null) { stmt.close(); } } }
private List<MovementVO> listQuery(PreparedStatement stmt) throws SQLException { ArrayList<MovementVO> searchResults = new ArrayList<MovementVO>(); ResultSet result = null; try { result = stmt.executeQuery(); while (result.next()) { MovementVO temp = new MovementVO(); temp.setId(result.getInt("CODIGO")); temp.setDate(Util.getDate(result.getDate("FECHA"))); temp.setAccount(Util.getAccountById(result.getString("CUENTA"))); temp.setValue(result.getDouble("VALOR")); temp.setConcept(result.getString("CONCEPTO")); temp.setObservation(result.getString("OBSERVACION")); searchResults.add(temp); } } finally { if (result != null) { result.close(); } if (stmt != null) { stmt.close(); } } return searchResults; }