Advertisement
Malnebi

Hotel

Sep 27th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package Hotel_Menagement;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Scanner;
  10.  
  11. import com.mysql.jdbc.PreparedStatement;
  12.  
  13. public class Hotel {
  14.     /*
  15.      * maja evo probaj ovaj dio ---pretrazivanje baze po imenu/broju lične
  16.      * karte/usernameu searchDatabase(String s); imas cak primjer na github
  17.      * linku ali jedino kod "na suho" mozes radit
  18.      */
  19.     public static void main(String[] args) throws Exception {
  20.  
  21.         // create scanner object, message to user and take user input
  22.         Scanner input = new Scanner(System.in);
  23.          System.out.println("Enter your name, username or identity card number: ");
  24.          String name = input.nextLine();
  25.          searchDatabase(name);
  26. //      inset info for testing
  27. //       Admin user = new Admin();
  28. //       user.enterInfo();
  29.  
  30.         System.out
  31.                 .println(" Enter user id you want to check out form the hotel.");
  32.         int id = input.nextInt();
  33.         userCheckOut1(id);
  34.  
  35.         input.close();
  36.     }
  37.  
  38.     // ID, FirstName, LastName, Gender, IdentityCard, Age, RoomNumber, RoomType,
  39.     // CheckInTime, UserName,Password
  40.  
  41.     /**
  42.      * @author Maja Vasilic
  43.      *
  44.      *         providing information from database based on entry
  45.      */
  46.     public static void searchDatabase(String s) throws Exception {
  47.         try {
  48.             // estalish connection to the database invoking method
  49.             // prepare statement
  50.             PreparedStatement statement = (PreparedStatement) getConnection()
  51.                     .prepareStatement(
  52.                             "SELECT * FROM information WHERE FirstName = ? OR IdentityCard= ? OR UserName = ?");
  53.  
  54.             // actual values are set to paramiters
  55.             statement.setString(1, s);
  56.             statement.setString(2, s);
  57.             statement.setString(3, s);
  58.  
  59.             // query execution
  60.             ResultSet result = statement.executeQuery();
  61.  
  62.             ResultSetMetaData rsmd = result.getMetaData();
  63.             int columnCount = rsmd.getColumnCount();
  64.  
  65.             String format = "%-20s";
  66.  
  67.             // print column names
  68.             for (int i = 1; i <= columnCount; i++) {
  69.                 System.out.printf(format, rsmd.getColumnLabel(i));
  70.             }
  71.             System.out.println();
  72.  
  73.             // print underline
  74.             for (int i = 1; i <= columnCount * 30; i++) {
  75.                 System.out.print("_");
  76.             }
  77.             System.out.println();
  78.  
  79.             // print information from database
  80.             while (result.next()) {
  81.                 for (int i = 1; i <= columnCount; i++) {
  82.                     System.out.printf(format, result.getString(i));
  83.                 }
  84.                 System.out.println();
  85.             }
  86.         } catch (Exception e) {
  87.             System.out.println(e);
  88.         }
  89.  
  90.     }
  91.  
  92.     /**
  93.      * author Maja Vasilic
  94.      *
  95.      * Method copying information from "information" table to the "archive"
  96.      * table and removes user from "information"
  97.      *
  98.      * @throws Exception
  99.      */
  100.     public static void userCheckOut(int id) throws Exception {
  101.         // declare object
  102.         Connection conn = null;
  103.         ResultSet result = null;
  104.         PreparedStatement st = null;
  105.         PreparedStatement stDelete = null;
  106.  
  107.         try {
  108.             // estalish connection to the database hotel invoking method       
  109.             conn = getConnection();
  110.  
  111.             /** FIND row with specified name */
  112.  
  113.             st = (PreparedStatement) conn
  114.                     .prepareStatement("SELECT * FROM `information` WHERE ID = "
  115.                             + id);
  116.             // Execute PreparedStatement and fill up ResultSet with result from
  117.             // the DB
  118.             result = st.executeQuery(); // user executeQuery for
  119.                                         // sql SELECT
  120.             result.next();
  121.             /**
  122.              * INSERT information from the information table to the archive
  123.              * table
  124.              */
  125.  
  126.             // create prepared statement
  127.             st = (PreparedStatement) conn
  128.                     .prepareStatement("INSERT INTO archive (ID,FirstName, LastName,"
  129.                             + " Gender, IdentityCard, Age) VALUES (?, ?, ?, ?, ?, ?)");
  130.  
  131.             System.out.println("Sending data to the archive.");
  132.  
  133.             // set values
  134.             st.setInt(1, result.getInt(1));
  135.             st.setString(2, result.getString(2));
  136.             st.setString(3, result.getString(3));
  137.             st.setString(4, result.getString(4));
  138.             st.setString(5, result.getString(5));
  139.             st.setInt(6, result.getInt(6));
  140.  
  141.             // execute query to insert values in the archive to the information
  142.             st.execute();
  143.  
  144.             /** DELETE row from the information table */
  145.  
  146.             // now we can remove user from current table for users information
  147.  
  148.             Statement s = conn.createStatement();
  149.  
  150.             s.executeUpdate("DELETE  FROM information WHERE ID = " + id + "");
  151.  
  152.             // print message
  153.             System.out.println("User " + result.getString(2) + " is removed.");
  154.             conn.close();
  155.  
  156.         } catch (Exception e) {
  157.             System.out.println(e);
  158.         } finally {
  159.             // close connection with the database
  160.             if (conn != null) {
  161.                 try {
  162.                     conn.close();
  163.                 } catch (SQLException e) {
  164.                     e.printStackTrace();
  165.                 }
  166.             }
  167.             if (result != null) {
  168.                 try {
  169.                     result.close();
  170.                 } catch (SQLException e) {
  171.                     e.printStackTrace();
  172.                 }
  173.             }
  174.             if (st != null) {
  175.                 try {
  176.                     st.close();
  177.                 } catch (SQLException e) {
  178.                     e.printStackTrace();
  179.                 }
  180.             }
  181.         }
  182.     }
  183.    
  184.  
  185.     /** establish connection with database */
  186.     public static Connection getConnection() throws Exception {
  187.         try {
  188.             // load jdbc driver
  189.             Class.forName("com.mysql.jdbc.Driver");
  190.  
  191.             // establish connection using url, username and password
  192.             Connection con = DriverManager.getConnection(
  193.                     "jdbc:mysql://localhost/hotel", "root", "maja");
  194.             return con;
  195.  
  196.         } catch (Exception e) {
  197.             System.out.println(e);
  198.         }
  199.         return null;
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement