Advertisement
Guest User

Untitled

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