daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Sep 26th, 2016 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.             Connection con = getConnection("hotel");
  48.             // var query with SQL query
  49.             String query = "SELECT * FROM hotel.information WHERE FirstName = ? OR IdentityCard= ? OR UserName = ?";
  50.             // create statement
  51.             PreparedStatement statement = (PreparedStatement) con
  52.                     .prepareStatement(query);
  53.  
  54.             // actual values are set to paramiters
  55.             statement.setString(1, s);
  56.             statement.setString(2, s);
  57.             statement.setString(3, s);
  58.             // query execution
  59.             ResultSet result = statement.executeQuery();
  60.  
  61.             ResultSetMetaData rsmd = result.getMetaData();
  62.             int columnCount = rsmd.getColumnCount();
  63.  
  64.             String format = "%-20s";
  65.  
  66.             // print column names
  67.             for (int i = 1; i <= columnCount; i++) {
  68.                 System.out.printf(format, rsmd.getColumnLabel(i));
  69.             }
  70.             System.out.println();
  71.  
  72.             // print underline
  73.             for (int i = 1; i <= columnCount * 30; i++) {
  74.                 System.out.print("_");
  75.             }
  76.             System.out.println();
  77.  
  78.             // print information from database
  79.             while (result.next()) {
  80.                 for (int i = 1; i <= columnCount; i++) {
  81.                     System.out.printf(format, result.getString(i));
  82.                 }
  83.                 System.out.println();
  84.             }
  85.         } catch (Exception e) {
  86.             System.out.println(e);
  87.         }
  88.  
  89.     }
  90.  
  91.     // ---odjava korisnika -MAJA
  92.     // searchDatabase(String name);
  93.     // osnovni podaci prvo kopirati u hotel_archive pa onda brisati korisnika
  94.  
  95.     /**
  96.      * author Maja Vasilic
  97.      *
  98.      * Method copying information from current database (hotel) to the archive
  99.      * (hotel_archive) and removes user from current
  100.      */
  101.     public static void userCheckOut(String name) {
  102.  
  103.         try {
  104.             // estalish connection to the database hotel invoking method
  105.             Connection conn = getConnection("hotel");
  106.  
  107.             // create statement variable
  108.             Statement stSelect = conn.createStatement();
  109.  
  110.             // query execution (find row with specified name)
  111.             ResultSet result = stSelect
  112.                     .executeQuery("SELECT * FROM hotel.information WHERE FirstName = '"
  113.                             + name + "'");
  114.  
  115.             System.out.println("Sending data to the archive.");
  116.  
  117.             // create connection with db hotel_archve
  118.             Connection conn1 = getConnection("hotel_archive");
  119.  
  120.             // create prepared statement to insert values in
  121.             // information.hotel_archive from information.hotel
  122.             PreparedStatement stInsert = (PreparedStatement) conn1
  123.                     .prepareStatement("INSERT INTO information.hotel_archive (FirstName, LastName,"
  124.                             + " Gender, IdentityCard, Age) VALUES (?, ?, ?, ?, ?)");
  125.  
  126.             // set values from one db to another
  127.             stInsert.setString(1, result.getString(2));
  128.             stInsert.setString(2, result.getString(3));
  129.             stInsert.setString(3, result.getString(4));
  130.             stInsert.setString(4, result.getString(5));
  131.             stInsert.setInt(5, result.getInt(6));
  132.  
  133.            
  134.            
  135.             // how we can remove user from current table for users
  136.             stSelect.executeQuery("DELETE FROM information.hotel WHERE FirstName = '"
  137.                     + name + "'");
  138.  
  139.             // print message
  140.             System.out.println("User " + name + " is removed.");
  141.  
  142.         } catch (Exception e) {
  143.             System.out.println(e);
  144.         }
  145.  
  146.     }
  147.  
  148.     /** establish connection with specified database */
  149.     public static Connection getConnection(String nameOfDB) throws Exception {
  150.         try {
  151.             // load jdbc driver
  152.             Class.forName("com.mysql.jdbc.Driver");
  153.  
  154.             // establish connection using url, username and password
  155.             Connection con = DriverManager.getConnection(
  156.                     "jdbc:mysql://localhost/"+nameOfDB, "root", "maja");
  157.             return con;
  158.  
  159.         } catch (Exception e) {
  160.             System.out.println(e);
  161.         }
  162.         return null;
  163.     }
  164. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top