public static boolean checkGuest(GuestEntities guest) { Statement stmt = null; String searchQuery = "Select * From guest where username = '******'"; boolean ableToCreate = false; // get the last member ID try { currentCon = DBConnectionManager.getConnection(); stmt = currentCon.createStatement(); rs = stmt.executeQuery(searchQuery); boolean more = rs.next(); if (!more) { ableToCreate = true; } else { ableToCreate = false; } } catch (Exception ex) { System.out.println("Registration failed: An Exception has occurred! " + ex); } // exception handling finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } rs = null; } if (stmt != null) { try { stmt.close(); } catch (Exception e) { } stmt = null; } if (currentCon != null) { try { currentCon.close(); } catch (Exception e) { } currentCon = null; } } return ableToCreate; }
public static GuestEntities guestRegister(GuestEntities guest) { Statement stmt = null; int maxId = 0; int currentId; // get the last member ID try { currentCon = DBConnectionManager.getConnection(); stmt = currentCon.createStatement(); String getMax = "select * from guest"; rs1 = stmt.executeQuery(getMax); while (rs1.next()) { String membershipNo = rs1.getString("membershipNo"); currentId = Integer.parseInt(membershipNo); if (currentId > maxId) { maxId = currentId; } } int nextId = maxId + 1; DecimalFormat df = new DecimalFormat("000000000"); String newMembershipNo; newMembershipNo = df.format(nextId).toString(); guest.setMembershipNo(newMembershipNo); currentCon = DBConnectionManager.getConnection(); stmt = currentCon.createStatement(); // query for inserting into the table String query = "insert into guest(userName, password, firstName, lastName, membershipNo, gender, nationality, dob, address, occupation, membership, points, roomNumber, cost) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; pstmt = currentCon.prepareStatement(query); // inserting values pstmt.setString(1, guest.getUserName()); pstmt.setString(2, guest.getPassword()); pstmt.setString(3, guest.getFirstName()); pstmt.setString(4, guest.getLastName()); pstmt.setString(5, newMembershipNo); pstmt.setString(6, guest.getGender()); pstmt.setString(7, guest.getNationality()); pstmt.setString(8, guest.getDob()); pstmt.setString(9, guest.getAddress()); pstmt.setString(10, guest.getOccupation()); pstmt.setString(11, guest.getMembership()); pstmt.setInt(12, guest.getPoints()); pstmt.setInt(13, guest.getRoomNo()); pstmt.setDouble(14, guest.getCost()); pstmt.executeUpdate(); } catch (Exception ex) { System.out.println("Registration failed: An Exception has occurred! " + ex); } // exception handling finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } rs = null; } if (stmt != null) { try { stmt.close(); } catch (Exception e) { } stmt = null; } if (currentCon != null) { try { currentCon.close(); } catch (Exception e) { } currentCon = null; } } return guest; }