public static Event getEvent(int eventId) { // SELECT * FROM Event WHERE eventId=eventId Connection con = null; Event event = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:8888/poly5", "root", ""); String sql = "SELECT * FROM event WHERE eventId=?;"; PreparedStatement preparedStatement = con.prepareStatement(sql); preparedStatement.setInt(1, eventId); ResultSet result = preparedStatement.executeQuery(); while (result.next()) { event = new Event(); event.cols = result.getInt("cols"); event.eventId = result.getInt("eventId"); event.eventName = result.getString("eventName"); event.rows = result.getInt("rows"); break; // only returns the first match } } catch (SQLException ex) { ex.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { // ignore } } } return event; }
public static ArrayList<Event> getAllEvents() { // SELECT * FROM Event Connection con = null; ArrayList<Event> events = new ArrayList<Event>(); try { con = DriverManager.getConnection("jdbc:mysql://localhost:8888/poly5", "root", ""); String sql = "SELECT * FROM event;"; PreparedStatement preparedStatement = con.prepareStatement(sql); ResultSet result = preparedStatement.executeQuery(); while (result.next()) { Event event = new Event(); event.cols = result.getInt("cols"); event.eventId = result.getInt("eventId"); event.eventName = result.getString("eventName"); event.rows = result.getInt("rows"); events.add(event); } } catch (SQLException ex) { ex.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { // ignore } } } return events; }