Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 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.     public static void main(String[] args) throws Exception {
  16.  
  17.         // create scanner object, message to user and take user input
  18.         Scanner input = new Scanner(System.in);
  19.         System.out
  20.                 .println("Enter your name, username or identity card number: ");
  21.         String name = input.nextLine();
  22.         searchDatabase(name);
  23.  
  24.         System.out
  25.                 .println(" Enter user name you want to check out form the hotel.");
  26.         String nameCheckOut = input.nextLine();
  27.         userCheckOut(nameCheckOut);
  28.  
  29.         input.close();
  30.     }
  31.  
  32.     // ID, First Name, Last Name, Gender, Identity Card, Age, Room Number, Room
  33.     // Type, CheckIn Time, User Name,Password
  34.  
  35.     /*
  36.      * @author Maja Vasilic
  37.      *
  38.      * providing information from database based on entry
  39.      */
  40.     public static void searchDatabase(String s) throws Exception {
  41.         try {
  42.             // estalish connection to the database invoking method
  43.             Connection con = getConnection();
  44.             // var query with SQL query
  45.             String query = "SELECT * FROM hotel.information WHERE FirstName = ? OR IdentityCard= ? OR UserName = ?";
  46.             // create statement
  47.             PreparedStatement statement = (PreparedStatement) con
  48.                     .prepareStatement(query);
  49.  
  50.             // actual values are set to paramiters
  51.             statement.setString(1, s);
  52.             statement.setString(2, s);
  53.             statement.setString(3, s);
  54.             // query execution
  55.             ResultSet result = statement.executeQuery();
  56.  
  57.             ResultSetMetaData rsmd = result.getMetaData();
  58.             int columnCount = rsmd.getColumnCount();
  59.  
  60.             String format = "%-20s";
  61.  
  62.             // print column names
  63.             for (int i = 1; i <= columnCount; i++) {
  64.                 System.out.printf(format, rsmd.getColumnLabel(i));
  65.             }
  66.             System.out.println();
  67.  
  68.             // print underline
  69.             for (int i = 1; i <= columnCount * 30; i++) {
  70.                 System.out.print("_");
  71.             }
  72.             System.out.println();
  73.  
  74.             // print information from database
  75.             while (result.next()) {
  76.                 for (int i = 1; i <= columnCount; i++) {
  77.                     System.out.printf(format, result.getString(i));
  78.                 }
  79.                 System.out.println();
  80.             }
  81.         } catch (Exception e) {
  82.             System.out.println(e);
  83.         }
  84.  
  85.     }
  86.  
  87.     /**
  88.      * author Maja Vasilic
  89.      *
  90.      * Method copying information from current database (hotel) to the archive
  91.      * (hotel_archive) and removes user from current
  92.      */
  93.     public static void userCheckOut(String name) {
  94.  
  95.         try {
  96.             // estalish connection to the database hotel invoking method
  97.             Connection conn = getConnection();
  98.  
  99.             // create statement variable
  100.             Statement stSelect = conn.createStatement();
  101.  
  102.             // query execution (find row with specified name)
  103.             ResultSet result = stSelect
  104.                     .executeQuery("SELECT * FROM hotel.information WHERE FirstName = '"
  105.                             + name + "'");
  106.  
  107.             System.out.println("Sending data to the archive.");
  108.  
  109.             // create connection with db hotel_archve
  110.             Connection conn1 = getConnection1();
  111.  
  112.             // create prepared statement to insert values in
  113.             // information.hotel_archive from information.hotel
  114.             PreparedStatement stInsert = (PreparedStatement) conn1
  115.                     .prepareStatement("INSERT INTO information.hotel_archive (FirstName, LastName,"
  116.                             + " Gender, IdentityCard, Age) VALUES (?, ?, ?, ?, ?)");
  117.  
  118.             // set values from one db to another
  119.             stInsert.setString(1, result.getString(2));
  120.             stInsert.setString(2, result.getString(3));
  121.             stInsert.setString(3, result.getString(4));
  122.             stInsert.setString(4, result.getString(5));
  123.             stInsert.setString(5, result.getString(6));
  124.  
  125.             // how we can remove user from current table for users
  126.             stSelect.executeQuery("DELETE FROM information.hotel WHERE FirstName = '"
  127.                     + name + "'");
  128.  
  129.             // print message
  130.             System.out.println("User " + name + " is removed.");
  131.  
  132.         } catch (Exception e) {
  133.             System.out.println(e);
  134.         }
  135.  
  136.     }
  137.  
  138.     /** establish connection with database hotel */
  139.     public static Connection getConnection() throws Exception {
  140.         try {
  141.             // load jdbc driver
  142.             Class.forName("com.mysql.jdbc.Driver");
  143.  
  144.             // establish connection using url, username and password
  145.             Connection con = DriverManager.getConnection(
  146.                     "jdbc:mysql://localhost/hotel", "root", "maja");
  147.             return con;
  148.  
  149.         } catch (Exception e) {
  150.             System.out.println(e);
  151.         }
  152.         return null;
  153.     }
  154.  
  155.     /** establish connection with database hotel_archive */
  156.     public static Connection getConnection1() throws Exception {
  157.         try {
  158.             // load jdbc driver
  159.             Class.forName("com.mysql.jdbc.Driver");
  160.  
  161.             // establish connection using url, username and password
  162.             Connection con = DriverManager.getConnection(
  163.                     "jdbc:mysql://localhost/hotel_archive", "root", "maja");
  164.             return con;
  165.  
  166.         } catch (Exception e) {
  167.             System.out.println(e);
  168.         }
  169.         return null;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement