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<>();
String query = "select * from calculo_fondo";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo"; try (PreparedStatement ps = conn.prepareStatement(query)) {
PreparedStatement ps = conn.prepareStatement(query); try (ResultSet rs = ps.executeQuery()) {
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); }
} catch (
rs.close(); SQLException e) {
ps.close();
} catch (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<>();
String query = "select * from calculo_fondo where caja_id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo where caja_id = ?"; try (PreparedStatement ps = conn.prepareStatement(query)) {
PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, caja.getId());
ps.setInt(1, caja.getId()); try (ResultSet rs = ps.executeQuery()) {
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;
String query = "select * from calculo_fondo where id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "select * from calculo_fondo where id = ?"; try (PreparedStatement ps = conn.prepareStatement(query)) {
PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, id);
ps.setInt(1, id); try (ResultSet rs = ps.executeQuery()) {
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,29 +103,27 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public boolean insertCalculoFondo(CalculoFondo calculoFondo) { public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
int updates; int updates;
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "insert into calculo_fondo (valor, descripcion, caja_id) values (?, ?, ?)"; try (PreparedStatement ps = conn.prepareStatement(query)) {
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());
updates = ps.executeUpdate(); updates = ps.executeUpdate();
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()) {
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
ps = conn.prepareStatement("select last_insert_rowid()"); rs.next();
ResultSet rs = ps.executeQuery(); calculoFondo.setId(rs.getInt(1));
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query); }
}
rs.next();
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,20 +134,19 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
@Override @Override
public boolean updateCalculoFondo(CalculoFondo calculoFondo) { public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
int updates; int updates;
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "update calculo_fondo set valor = ?, descripcion = ?, caja_id = ? where id = ?"; try (PreparedStatement ps = conn.prepareStatement(query)) {
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()); ps.setInt(4, calculoFondo.getId());
ps.setInt(4, calculoFondo.getId());
updates = ps.executeUpdate(); updates = ps.executeUpdate();
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;
String query = "delete from calculo_fondo where id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "delete from calculo_fondo where id = ?"; try (PreparedStatement ps = conn.prepareStatement(query)) {
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;
String query = "select sum(valor) from calculo_fondo where caja_id = ?";
try (Connection conn = connectionHolder.getConnection()) { try (Connection conn = connectionHolder.getConnection()) {
String query = "select sum(valor) from calculo_fondo where caja_id = ?"; try (PreparedStatement ps = conn.prepareStatement(query)) {
PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, caja.getId());
ps.setInt(1, caja.getId()); try (ResultSet rs = ps.executeQuery()) {
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);
rs.next(); }
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);
} }