PreparedStatements y ResultSets en try/catch
Mas seguridad en estos elementos, mas que nada para que se cierren correctamente.
This commit is contained in:
@@ -46,18 +46,17 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
|
||||
@Override
|
||||
public List<CalculoFondo> findAll() {
|
||||
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
String query = "select * from calculo_fondo";
|
||||
PreparedStatement ps = conn.prepareStatement(query);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
try (PreparedStatement ps = conn.prepareStatement(query)) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0}", query);
|
||||
|
||||
calculoFondoList = this.calculoFondoFromResultSet(rs);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
} catch (
|
||||
SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
return calculoFondoList;
|
||||
@@ -66,18 +65,16 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
|
||||
@Override
|
||||
public List<CalculoFondo> findByCaja(Caja caja) {
|
||||
List<CalculoFondo> calculoFondoList = new ArrayList<>();
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
||||
|
||||
calculoFondoList = this.calculoFondoFromResultSet(rs);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, id});
|
||||
|
||||
calculoFondo = this.calculoFondoFromResultSet(rs).get(0);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
@@ -108,9 +103,9 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
|
||||
@Override
|
||||
public boolean insertCalculoFondo(CalculoFondo calculoFondo) {
|
||||
int updates;
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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.setString(2, calculoFondo.getDescripcion());
|
||||
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}",
|
||||
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
|
||||
calculoFondo.getCaja().getId(), updates});
|
||||
}
|
||||
|
||||
ps.close();
|
||||
|
||||
ps = conn.prepareStatement("select last_insert_rowid()");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
try (PreparedStatement ps = conn.prepareStatement("select last_insert_rowid()")) {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "Se ejecuta query: {0}", query);
|
||||
|
||||
rs.next();
|
||||
calculoFondo.setId(rs.getInt(1));
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
return false;
|
||||
@@ -141,9 +134,9 @@ public class SQLiteCalculoFondoDAO extends CalculoFondoDAO {
|
||||
@Override
|
||||
public boolean updateCalculoFondo(CalculoFondo calculoFondo) {
|
||||
int updates;
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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.setString(2, calculoFondo.getDescripcion());
|
||||
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}",
|
||||
new Object[]{query, calculoFondo.getValor(), calculoFondo.getDescripcion(),
|
||||
calculoFondo.getCaja().getId(), updates});
|
||||
|
||||
ps.close();
|
||||
}
|
||||
} 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;
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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());
|
||||
updates = ps.executeUpdate();
|
||||
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1} | updates: {2}",
|
||||
new Object[]{query, calculoFondo.getId(), updates});
|
||||
|
||||
ps.close();
|
||||
}
|
||||
} 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;
|
||||
try (Connection conn = connectionHolder.getConnection()) {
|
||||
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());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
LOGGER.log(Level.FINE, "QUERY: {0} | values: {1}", new Object[]{query, caja.getId()});
|
||||
|
||||
rs.next();
|
||||
sum = rs.getInt(1);
|
||||
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user