/* (non-Javadoc) * @see assn2.daos.RecordDAO#getAllByUser(int) */ @Override public List<RecordBean> getAllByHotel(int id) throws DataAccessException { Connection conn = null; List<RecordBean> list = new ArrayList<RecordBean>(); try { conn = services.createConnection(); PreparedStatement stmt = conn.prepareStatement("select * from Record where hotelid = ? "); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); if (rs == null) // remember to catch the exceptions throw new DataAccessException("cannot find entity of that userid"); while (rs.next()) { RecordBean r = createRecordBean(rs); list.add(r); } } catch (ServiceLocatorException e) { e.printStackTrace(); // no connection } catch (SQLException e) { e.printStackTrace(); // not execution of statement } finally { if (conn != null) { try { conn.close(); // and close the connections etc } catch (SQLException e1) { // if not close properly e1.printStackTrace(); } } } return list; }
public int getUserid(int recordid) { Connection conn = null; int r = -1; try { conn = services.createConnection(); PreparedStatement stmt = conn.prepareStatement( "select * from Record as r, Booking as b where r.bookingid = b.bookingid AND r.recordid = (?) "); stmt.setInt(1, recordid); ResultSet rs = stmt.executeQuery(); if (rs == null) // remember to catch the exceptions throw new DataAccessException("cannot find entity of that recordid"); rs.next(); r = rs.getInt("userid"); return r; } catch (ServiceLocatorException e) { e.printStackTrace(); // no connection } catch (SQLException e) { e.printStackTrace(); // not execution of statement } finally { if (conn != null) { try { conn.close(); // and close the connections etc } catch (SQLException e1) { // if not close properly e1.printStackTrace(); } } } return r; }
/* (non-Javadoc) * @see assn2.daos.RecordDAO#getRecord(int) */ @Override public RecordBean getRecord(int id) throws DataAccessException { Connection conn = null; try { conn = services.createConnection(); PreparedStatement stmt = conn.prepareStatement("select * from Record where recordid = ? "); stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); if (rs == null) // remember to catch the exceptions throw new DataAccessException("cannot find entity of that recordid"); if (rs.next()) { RecordBean r = createRecordBean(rs); stmt.close(); // close it return r; } } catch (ServiceLocatorException e) { e.printStackTrace(); // no connection } catch (SQLException e) { e.printStackTrace(); // not execution of statement } finally { if (conn != null) { try { conn.close(); // and close the connections etc } catch (SQLException e1) { // if not close properly e1.printStackTrace(); } } } return null; // not found }
/* (non-Javadoc) * @see assn2.daos.RecordDAO#deleteAllByBooking(int) */ @Override public void deleteAllByBooking(int id) throws DataAccessException { Connection con = null; try { // get the connection con = services.createConnection(); PreparedStatement stmt = con.prepareStatement("delete from Record where bookingid=(?)"); stmt.setInt(1, id); int status = stmt.executeUpdate(); // if (status == 0)//remember to catch the exceptions // throw new DataAccessException("cannot delete any record owned by that id"); // } catch (ServiceLocatorException e) { throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e); } catch (SQLException e) { throw new DataAccessException("Unable to execute query; " + e.getMessage(), e); } finally { if (con != null) { try { con.close(); // and close the connections etc } catch (SQLException e1) { // if not close properly e1.printStackTrace(); } } } }
public void insert( int bookingid, int hotelid, int roomtypeid, int extrabed, double price, Timestamp checkindate, Timestamp checkoutdate) throws DataAccessException { Connection con = null; try { // get the connection con = services.createConnection(); PreparedStatement stmt = con.prepareStatement( "Insert Into Record (bookingid, hotelid, roomtypeid, extrabed, " + "price, checkindate, checkoutdate) " + "values (?, ?, ?, ?, ?, ?,?)"); stmt.setInt(1, bookingid); stmt.setInt(2, hotelid); stmt.setInt(3, roomtypeid); stmt.setInt(4, extrabed); // stmt.setInt(5, requestamount); stmt.setDouble(5, price); stmt.setTimestamp(6, checkindate); stmt.setTimestamp(7, checkoutdate); // execute the update int n = stmt.executeUpdate(); if (n != 1) // remember to catch the exceptions throw new DataAccessException("Did not insert one row into database"); } catch (ServiceLocatorException e) { throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e); } catch (SQLException e) { throw new DataAccessException("Unable to execute query; " + e.getMessage(), e); } finally { if (con != null) { try { con.close(); // and close the connections etc } catch (SQLException e1) { // if not close properly e1.printStackTrace(); } } } }