private void insertFootBaller(FootBaller footBaller) { try { String stmtInsert = String.format(STATEMENT_INSERT, "footballers", "?,?"); PreparedStatement stmt = prepareStatement(stmtInsert); stmt.setLong(1, footBaller.getId()); stmt.setString(2, footBaller.getClub()); stmt.executeUpdate(); } catch (SQLException e) { throw new AppRuntimeException(e); } }
private void updateFootBaller(FootBaller footBaller) { try { String stmtUpdate = String.format(STATEMENT_UPDATE, "footballers", "club=?"); PreparedStatement stmt = prepareStatement(stmtUpdate); stmt.setString(1, footBaller.getClub()); stmt.setLong(2, footBaller.getId()); stmt.executeUpdate(); } catch (SQLException e) { throw new AppRuntimeException(e); } }
public Player find(long id) { try { ResultSet playerRs = findRow(id, "players"); String name = playerRs.getString("name"); String typeString = playerRs.getString("type"); TYPE type = TYPE.valueOf(typeString); switch (type) { case FOOTBALL: { ResultSet footBallRs = findRow(id, "footballers"); FootBaller footBaller = new FootBaller(); footBaller.setId(id); footBaller.setName(name); String club = footBallRs.getString("club"); footBaller.setClub(club); return footBaller; } case CRICKET: { ResultSet cricketRs = findRow(id, "cricketers"); Cricketer criketer = new Cricketer(); criketer.setId(id); criketer.setName(name); long battingAverage = cricketRs.getLong("batting_average"); criketer.setBattingAverage(battingAverage); return criketer; } case BOWLING: { ResultSet bowlingRs = findRow(id, "bowlers"); ResultSet cricketRs = findRow(id, "cricketers"); Bowler bowler = new Bowler(); bowler.setId(id); bowler.setName(name); long battingAverage = cricketRs.getLong("batting_average"); long bowlingAverage = bowlingRs.getLong("bowling_average"); bowler.setBattingAverage(battingAverage); bowler.setBowlingAverage(bowlingAverage); return bowler; } default: throw new AppRuntimeException("unknown type"); } } catch (SQLException e) { throw new AppRuntimeException(e); } }