Advertisement
daxing8

Grupni Projekt Hotel

Sep 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. package grupniProjektHotel;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9.  
  10. public class Admin {
  11.  
  12. public int ID, age, roomNum, balance;
  13. public String firstName, lastName, gender, identityCard, userName, password, roomType;
  14. public String checkInTime, checkOutTime, service, loggedIn;
  15. private static Connection conection;
  16.  
  17. /*
  18. * @author Jasmin Bektic
  19. *
  20. * Method confirms connection
  21. */
  22. public Connection getConnection() {
  23. try {
  24. String driver = "com.mysql.jdbc.Driver";
  25. String url = "jdbc:mysql://localhost:3306/hotel";
  26. String user = "root";
  27. String pass = "";
  28. Class.forName(driver);
  29.  
  30. Connection con = DriverManager.getConnection(url, user, pass); // Connecting
  31. // with
  32. // provided
  33. // url,userName
  34. // and
  35. // pass
  36. return con;
  37. } catch (Exception e) { // Catch errors
  38. System.out.println(e);
  39. }
  40. return null;
  41. }
  42.  
  43. public void checkGuest() {
  44.  
  45. }
  46.  
  47. public void enterInfo() throws SQLException {
  48. Connection con = getConnection();
  49. String sql = "INSERT INTO information (information.FirstName,'lastName',Information.Gender,'identityCard','age','roomNum','roomType','checkInTime','userName','password') VALUES (?,?,?,?,?,?,?,?,?,?)";
  50. PreparedStatement statement = con.prepareStatement(sql);
  51. // statement.setString(1, s);
  52. // statement.setString(2, s);
  53. // statement.setString(3, s);
  54. statement.executeUpdate(sql);
  55. // ResultSet result = statement.executeQuery();
  56. // ResultSetMetaData rsmd = result.getMetaData();
  57. }
  58.  
  59. /*
  60. * @ author Davor Sadikovic
  61. *
  62. * Method prints occupied rooms and prompts user to enter room number
  63. * Retruns room number
  64. */
  65. public int checkAvailableRoom() throws SQLException {
  66.  
  67. Scanner input = new Scanner(System.in);
  68. int roomNumber = 0;
  69.  
  70. try {
  71. //Connects to server
  72. Connection conn = getConnection();
  73. String query = "SELECT Room Number AND Room Type FROM information";
  74. PreparedStatement ps = conn.prepareStatement(query);
  75.  
  76. ResultSet rs = ps.executeQuery();
  77.  
  78. //Prints occupied rooms
  79. System.out.println("Rooms that are occupied are: \n");
  80. while (rs.next()) {
  81. System.out.println("Room number: " + rs.getInt(roomNum) + "\tRoom Type: " + rs.getString("Room Type"));
  82. }
  83.  
  84. //Prompts user to enter a room number
  85. System.out.println("\nEnter room number: ");
  86. roomNumber = input.nextInt();
  87.  
  88. //Checks if room number is occupied and if it is it calls method again
  89. while (rs.next()) {
  90. if (roomNumber == rs.getInt(roomNum)) {
  91. System.out.println("Room number you entered is occupied. ");
  92. checkAvailableRoom();
  93. }
  94. }
  95. input.close();
  96.  
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. return roomNumber;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement