PreparedStatements y ResultSets en try/catch

Mas seguridad en estos elementos, mas que nada para que se cierren
correctamente.
This commit is contained in:
Daniel Cortes
2019-03-07 01:48:23 -03:00
parent e446cc5bb0
commit db9568c85f

View File

@@ -46,18 +46,17 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public List<CalculoFondo> findAll() { public List<CalculoFondo> findAll() {
List<CalculoFondo> calculoFondoList = new ArrayList<>(); List<CalculoFondo> calculoFondoList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo"; String query = "select * from calculo_fondo";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
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); calculoFondoList = this.calculoFondoFromResultSet(rs);
}
rs.close(); }
ps.close(); } catch (
} catch (SQLException e) { SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
} }
return calculoFondoList; return calculoFondoList;
@@ -66,18 +65,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public List<CalculoFondo> findByCaja(Caja caja) { public List<CalculoFondo> findByCaja(Caja caja) {
List<CalculoFondo> calculoFondoList = new ArrayList<>(); List<CalculoFondo> calculoFondoList = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo where caja_id = ?"; String query = "select * from calculo_fondo where caja_id = ?";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, caja.getId()); ps.setInt(1, caja.getId());
ResultSet rs = ps.executeQuery(); 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); calculoFondoList = this.calculoFondoFromResultSet(rs);
}
rs.close(); }
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
} }
@@ -87,18 +84,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public CalculoFondo findById(int id) { public CalculoFondo findById(int id) {
CalculoFondo calculoFondo = null; CalculoFondo calculoFondo = null;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo where id = ?"; String query = "select * from calculo_fondo where id = ?";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, id); ps.setInt(1, id);
ResultSet rs = ps.executeQuery(); 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); calculoFondo = this.calculoFondoFromResultSet(rs).get(0);
}
rs.close(); }
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
} }
@@ -108,9 +103,9 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public boolean insertCalculoFondo(CalculoFondo calculoFondo) { public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
int updates; int updates;
try (Connection conn = connectionHolder.getConnection()) {
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, calculoFondo.getValor()); ps.setInt(1, calculoFondo.getValor());
ps.setString(2, calculoFondo.getDescripcion()); ps.setString(2, calculoFondo.getDescripcion());
ps.setInt(3, calculoFondo.getCaja().getId()); ps.setInt(3, calculoFondo.getCaja().getId());
@@ -119,18 +114,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}] | updates: {4}", LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}] | updates: {4}",
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
calculoFondo.getCaja().getId(), updates}); calculoFondo.getCaja().getId(), updates});
}
ps.close(); try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
try (ResultSet rs = ps.executeQuery()) {
ps = conn.prepareStatement("select last_insert_rowid()");
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query); LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
rs.next(); rs.next();
calculoFondo.setId(rs.getInt(1)); calculoFondo.setId(rs.getInt(1));
}
rs.close(); }
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
return false; return false;
@@ -141,9 +134,9 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public boolean updateCalculoFondo(CalculoFondo calculoFondo) { public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
int updates; int updates;
try (Connection conn = connectionHolder.getConnection()) {
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?"; String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, calculoFondo.getValor()); ps.setInt(1, calculoFondo.getValor());
ps.setString(2, calculoFondo.getDescripcion()); ps.setString(2, calculoFondo.getDescripcion());
ps.setInt(3, calculoFondo.getCaja().getId()); ps.setInt(3, calculoFondo.getCaja().getId());
@@ -153,8 +146,7 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}, {4}] | updates: {5}", LOGGER.log(Level.FINE, "QUERY {0} | values: [{1}, {2}, {3}, {4}] | updates: {5}",
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(), new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
calculoFondo.getCaja().getId(), updates}); calculoFondo.getCaja().getId(), updates});
}
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
return false; return false;
@@ -165,16 +157,15 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public boolean deleteCalculoFondo(CalculoFondo calculoFondo) { public boolean deleteCalculoFondo(CalculoFondo calculoFondo) {
int updates; int updates;
try (Connection conn = connectionHolder.getConnection()) {
String query = "delete from calculo_fondo where id = ?"; String query = "delete from calculo_fondo where id = ?";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, calculoFondo.getId()); ps.setInt(1, calculoFondo.getId());
updates = ps.executeUpdate(); updates = ps.executeUpdate();
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}", LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}",
new Object[]{query, calculoFondo.getId(), updates}); new Object[]{query, calculoFondo.getId(), updates});
}
ps.close();
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
return false; return false;
@@ -185,18 +176,17 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public int getTotalCalculoFondo(Caja caja) { public int getTotalCalculoFondo(Caja caja) {
int sum = 0; int sum = 0;
try (Connection conn = connectionHolder.getConnection()) {
String query = "select sum(valor) from calculo_fondo where caja_id = ?"; String query = "select sum(valor) from calculo_fondo where caja_id = ?";
PreparedStatement ps = conn.prepareStatement(query); try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, caja.getId()); ps.setInt(1, caja.getId());
ResultSet rs = ps.executeQuery(); 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(); rs.next();
sum = rs.getInt(1); sum = rs.getInt(1);
}
ps.close(); }
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e); LOGGER.log(Level.SEVERE, e.toString(), e);
} }