public void save() { for (Pair<String, Integer> pair : portfolio) { try { Connection con = (Connection) DatabaseConnection.getConnection(); PreparedStatement ps; if (newlyAdded.contains(pair)) { ps = con.prepareStatement( "INSERT INTO `maplestocks_data` (`cid`, `stockid`, `shares`) VALUES (?, ?, ?)"); ps.setInt(1, cid); ps.setInt(2, MapleStocks.getInstance().idOf(pair.getLeft())); ps.setInt(3, pair.getRight()); Output.print(ps.toString()); ps.executeUpdate(); } else { ps = con.prepareStatement( "UPDATE maplestocks_data SET shares = ? WHERE cid = ? AND stockid = ?"); ps.setInt(1, pair.getRight()); ps.setInt(2, cid); ps.setInt(3, MapleStocks.getInstance().idOf(pair.getLeft())); Output.print(ps.toString()); ps.executeUpdate(); } } catch (SQLException e) { Output.print("Something went wrong while saving a MapleStockPortfolio."); GameLogger.print(GameLogger.EXCEPTION_CAUGHT, e); } } }
public static MapleStockPortfolio load(int cid) { MapleStockPortfolio ret = new MapleStockPortfolio(cid); try { Connection con = (Connection) DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT * FROM `maplestocks_data` WHERE `cid` = ?"); ps.setInt(1, cid); ResultSet rs = ps.executeQuery(); while (rs.next()) { ret.add( new Pair<String, Integer>( MapleStocks.getInstance().tickerOf(rs.getInt("stockid")), rs.getInt("shares"))); } } catch (SQLException e) { Output.print("Failed to load MapleStockPortfolio " + cid + "."); GameLogger.print(GameLogger.EXCEPTION_CAUGHT, e); } return ret; }