From db9568c85f16f0ca70d77334ef7eeb6c032d33af Mon Sep 17 00:00:00 2001 From: Daniel Cortes Date: Thu, 7 Mar 2019 01:48:23 -0300 Subject: [PATCH] PreparedStatements y ResultSets en try/catch Mas seguridad en estos elementos, mas que nada para que se cierren correctamente. --- .../calculo_fondo/SQLiteCalculoFondoDAO.java | 148 ++++++++---------- 1 file changed, 69 insertions(+), 79 deletions(-) diff --git a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java index 6236e09..5426479 100644 --- a/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java +++ b/src/danielcortes/xyz/models/calculo_fondo/SQLiteCalculoFondoDAO.java @@ -46,18 +46,17 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public List findAll() { List calculoFondoList = new ArrayList<>(); + String query = "select * from calculo_fondo"; try (Connection conn = connectionHolder.getConnection()) { - String query = "select * from calculo_fondo"; - PreparedStatement ps = conn.prepareStatement(query); - ResultSet rs = ps.executeQuery(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "QUERY: {0}", query); - LOGGER.log(Level.FINE, "QUERY: {0}", query); - - calculoFondoList = this.calculoFondoFromResultSet(rs); - - rs.close(); - ps.close(); - } catch (SQLException e) { + calculoFondoList = this.calculoFondoFromResultSet(rs); + } + } + } catch ( + SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); } return calculoFondoList; @@ -66,18 +65,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public List findByCaja(Caja caja) { List calculoFondoList = new ArrayList<>(); + String query = "select * from calculo_fondo where caja_id = ?"; try (Connection conn = connectionHolder.getConnection()) { - String query = "select * from calculo_fondo where caja_id = ?"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, caja.getId()); - ResultSet rs = ps.executeQuery(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, caja.getId()); + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); - LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); - - calculoFondoList = this.calculoFondoFromResultSet(rs); - - rs.close(); - ps.close(); + calculoFondoList = this.calculoFondoFromResultSet(rs); + } + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); } @@ -87,18 +84,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public CalculoFondo findById(int id) { CalculoFondo calculoFondo = null; + String query = "select * from calculo_fondo where id = ?"; try (Connection conn = connectionHolder.getConnection()) { - String query = "select * from calculo_fondo where id = ?"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, id); + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id}); - LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id}); - - calculoFondo = this.calculoFondoFromResultSet(rs).get(0); - - rs.close(); - ps.close(); + calculoFondo = this.calculoFondoFromResultSet(rs).get(0); + } + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); } @@ -108,29 +103,27 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public boolean insertCalculoFondo(CalculoFondo calculoFondo) { int updates; + String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; try (Connection conn = connectionHolder.getConnection()) { - String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, calculoFondo.getValor()); - ps.setString(2, calculoFondo.getDescripcion()); - ps.setInt(3, calculoFondo.getCaja().getId()); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, calculoFondo.getValor()); + ps.setString(2, calculoFondo.getDescripcion()); + ps.setInt(3, calculoFondo.getCaja().getId()); - updates = ps.executeUpdate(); - LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}] | updates: {4}", - new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), - calculoFondo.getCaja().getId(), updates}); + updates = ps.executeUpdate(); + LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}] | updates: {4}", + new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), + calculoFondo.getCaja().getId(), updates}); + } - ps.close(); + try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) { + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query); - ps = conn.prepareStatement("select last_insert_rowid()"); - ResultSet rs = ps.executeQuery(); - LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query); - - rs.next(); - calculoFondo.setId(rs.getInt(1)); - - rs.close(); - ps.close(); + rs.next(); + calculoFondo.setId(rs.getInt(1)); + } + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); return false; @@ -141,20 +134,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public boolean updateCalculoFondo(CalculoFondo calculoFondo) { int updates; + String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?"; try (Connection conn = connectionHolder.getConnection()) { - String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, calculoFondo.getValor()); - ps.setString(2, calculoFondo.getDescripcion()); - ps.setInt(3, calculoFondo.getCaja().getId()); - ps.setInt(4, calculoFondo.getId()); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, calculoFondo.getValor()); + ps.setString(2, calculoFondo.getDescripcion()); + ps.setInt(3, calculoFondo.getCaja().getId()); + ps.setInt(4, calculoFondo.getId()); - updates = ps.executeUpdate(); - LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}, {4}] | updates: {5}", - new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), - calculoFondo.getCaja().getId(), updates}); - - ps.close(); + updates = ps.executeUpdate(); + LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}, {4}] | updates: {5}", + new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), + calculoFondo.getCaja().getId(), updates}); + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); return false; @@ -165,16 +157,15 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public boolean deleteCalculoFondo(CalculoFondo calculoFondo) { int updates; + String query = "delete from calculo_fondo where id = ?"; try (Connection conn = connectionHolder.getConnection()) { - String query = "delete from calculo_fondo where id = ?"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, calculoFondo.getId()); - updates = ps.executeUpdate(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, calculoFondo.getId()); + updates = ps.executeUpdate(); - LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}", - new Object[]{query, calculoFondo.getId(), updates}); - - ps.close(); + LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}", + new Object[]{query, calculoFondo.getId(), updates}); + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); return false; @@ -185,18 +176,17 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO { @Override public int getTotalCalculoFondo(Caja caja) { int sum = 0; + String query = "select sum(valor) from calculo_fondo where caja_id = ?"; try (Connection conn = connectionHolder.getConnection()) { - String query = "select sum(valor) from calculo_fondo where caja_id = ?"; - PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, caja.getId()); - ResultSet rs = ps.executeQuery(); + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(1, caja.getId()); + try (ResultSet rs = ps.executeQuery()) { + LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); - LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()}); - - rs.next(); - sum = rs.getInt(1); - - ps.close(); + rs.next(); + sum = rs.getInt(1); + } + } } catch (SQLException e) { LOGGER.log(Level.SEVERE, e.toString(), e); }