@Override public ManagerPanel getById(int userId) throws DbException { PreparedStatement ps = null; ResultSet rs = null; ManagerPanel managerPanel = null; try { managerPanel = new ManagerPanel(); prepareConnection(); ps = connection.prepareStatement(MANAGER_SELECT_BY_ID); ps.setInt(1, userId); rs = ps.executeQuery(); rs.next(); Money finresult = Money.dollars(rs.getDouble("summaryfinresult")); managerPanel.setSummaryFinRes(finresult); } catch (SQLException e) { throw new DbException("Can't execute SQL = '" + MANAGER_SELECT_BY_ID + "'", e); } finally { daoHelper.closeDataBaseEntities(ps, rs, connection); } return managerPanel; }
@Override public long insert(ManagerPanel panel, int userId) throws DbException { PreparedStatement ps = null; long result; try { prepareConnection(); ps = connection.prepareStatement(MANAGER_INSERT, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, userId); ps.setDouble(2, panel.getSummaryFinRes().getAmount()); ps.executeUpdate(); try (ResultSet rs = ps.getGeneratedKeys()) { if (rs.next()) { result = rs.getLong(1); } else { throw new DbException( "Inserting failed on query: '" + MANAGER_INSERT + "' no ID obtained."); } } } catch (SQLException e) { throw new DbException("Can't execute SQL = '" + MANAGER_INSERT + "'", e); } finally { daoHelper.closeDataBaseEntities(ps, null, connection); } return result; }
@Override public void deleteById(int userId) throws DbException { PreparedStatement ps = null; try { prepareConnection(); ps = connection.prepareStatement(MANAGER_DELETE_BY_ID); ps.setInt(1, userId); ps.executeUpdate(); } catch (SQLException e) { throw new DbException("Can't execute SQL = '" + MANAGER_DELETE_BY_ID + "'", e); } finally { daoHelper.closeDataBaseEntities(ps, null, connection); } }
@Override public void update(ManagerPanel panel, int userId) throws DbException { PreparedStatement ps = null; try { prepareConnection(); ps = connection.prepareStatement(MANAGER_UPDATE); ps.setDouble(1, panel.getSummaryFinRes().getAmount()); ps.setInt(2, userId); ps.executeUpdate(); } catch (SQLException e) { throw new DbException("Can't execute SQL = '" + MANAGER_UPDATE + "'", e); } finally { daoHelper.closeDataBaseEntities(ps, null, connection); } }
private void prepareConnection() { daoHelper = new DaoHelper(); daoHelper.initConn(); connection = daoHelper.getConnection(); }