Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.81 KB | None | 0 0
  1. /**
  2.  * Created by Edward on 13/12/2016.
  3.  */
  4. package connectionMySql;
  5.  
  6. import java.io.*;
  7. import java.sql.*;
  8. import java.util.Scanner;
  9.  
  10. public class ConnectionMySql {
  11.     static {
  12.         try { /* Type 4 Driver */
  13.             Class.forName("com.mysql.jdbc.Driver"); //TODO? lib
  14.         } catch (ClassNotFoundException e) {
  15.             System.err.println("Could not load MySql driver.");
  16.             System.err.println(e.getMessage());
  17.             System.exit(1);
  18.         }
  19.     }
  20.  
  21.     public static void main(String args[]) {
  22.         String uname = null; //TODO vul uw username van uw databank in
  23.         String psswrd = null; //TODO vul uw password van uw user van uw databank in
  24.         Integer choice = 1; /* Location of the database */
  25.         String host = "jdbc:mysql://localhost/orderdb"; //TODO
  26.         String query = "SELECT * FROM restaurant";
  27.         /* Reading log-in data (username and password) */
  28.         try {
  29.             BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  30.             System.out.print("Enter your username on MySql: ");
  31.             uname = br1.readLine();
  32.             BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
  33.             System.out.print("Enter your password on MySql: ");
  34.             psswrd = br2.readLine();
  35.         } catch (IOException e) {
  36.             System.out.print("Failed to get uname/passwd");
  37.             System.out.println(":" + e.getMessage());
  38.             System.exit(1);
  39.         }
  40.         /* Connect to MySql database */
  41.         try {
  42.             Connection conn = DriverManager.getConnection(host, uname, psswrd);
  43.             System.out.println("Connection established...");
  44.             System.out.println(); /* Create statement */
  45.             Statement stmt = conn.createStatement(); /* Execute the query */
  46.             ResultSet rs = stmt.executeQuery(query); /* Output */
  47.             System.out.println("This is an example how you query a DBMS.");
  48.             System.out.println();
  49.             System.out.println(query);
  50.             System.out.println("ID // First Name // Last Name");
  51.             System.out.println("------------------------------");
  52.             while (rs.next()) {
  53.                 System.out.print(rs.getString(1));
  54.                 System.out.print((" // "));
  55.                 System.out.print(rs.getString(2));
  56.                 System.out.print((" // "));
  57.                 System.out.print(rs.getString(3));
  58.                 System.out.print((" // "));
  59.                 System.out.println(rs.getString(4)); //dees ga crashe, from restaurent heeft 3 kolomme geen 4 zoals in customer.. losers
  60.             }
  61.             rs.close();
  62.             stmt.close();
  63.             conn.close();
  64.         } catch (SQLException e) {
  65.             System.out.println("SQL Exception: ");
  66.             System.err.println(e.getMessage());
  67.         }
  68.         /* Example of choice options */
  69.         while (choice != 0) {
  70.             Scanner s = new Scanner(System.in); // ni zo goe als BufferedReader(=synchronized en meer performant?), maar ez code
  71.             /*Ask for user's choice*/
  72.             try {
  73.                 BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  74.                 System.out.println();
  75.                 System.out.println("This is an example of a choice menu.");
  76.                 System.out.println();
  77.                 System.out.println("Please choose between the following options:");
  78.                 System.out.println("     (1) Add registration");
  79.                 System.out.println("     (2) Show popular sessions list");
  80.                 System.out.println("     (0) Quit");
  81.                 System.out.print("Enter your choice: ");
  82.                 choice = Integer.parseInt(br1.readLine());
  83.             } catch (NumberFormatException ex) {
  84.                 System.err.println("Not a valid number");
  85.             } catch (IOException e) {
  86.                 e.printStackTrace();
  87.             }
  88.             if (choice == 1) {
  89.                 System.out.println("What is your customer id? : ");
  90.                 int customerId = s.nextInt();
  91.  
  92.                 //uw query om alles "*" uit uw productentabel te halen
  93.                 //ASC is default bij ORDER BY ma snapte wardje doe graaf om da aan te tone
  94.                 String _query = "SELECT * FROM PRODUCT ORDER BY Category ASC, Name ASC;";
  95.                 System.out.println();
  96.                 System.out.println("Product list:");
  97.                 System.out.println("ID - Product Name - Category"); //mooi geformatteerde vorm, ni amateuristisch, tzzzz wollah
  98.                 System.out.println("****************************************** ");
  99.                 System.out.println();
  100.                 try {//toont heel de lijst van producten
  101.                     Connection conn = DriverManager.getConnection(host, uname, psswrd);
  102.                     Statement stmt = conn.createStatement(); /* Execute the query */
  103.                     ResultSet rs = stmt.executeQuery(_query); /* Output */
  104.                     while (rs.next()) {
  105.                         String id = rs.getString(1);
  106.                         String prodName = rs.getString(2);
  107.                         String cat = rs.getString(3);
  108.                         System.out.println();// mooi geformateerd
  109.                     }
  110.                     rs.close();
  111.                     stmt.close();
  112.                     conn.close();
  113.                 } catch (SQLException e) {
  114.                     e.printStackTrace();
  115.                 }
  116.  
  117.                 System.out.println();
  118.                 System.out.println("Which product do you want to order? : ");
  119.                 int productId = s.nextInt();
  120.                 _query = "SELECT * FROM OFFERS WHERE Product Id=" + productId + ";";
  121.                 try {
  122.                     Connection conn = DriverManager.getConnection(host, uname, psswrd);
  123.                     Statement stmt = conn.createStatement(); /* Execute the query */
  124.                     ResultSet rs = stmt.executeQuery(_query); /* Output */
  125.                     int highestRating = 0;
  126.                     int restIdWithHighestRating = -1; //-1 om aan te tonen dat er een error is als uiteindelijk de id op -1 blijft (moet ni per se)
  127.                     String restoNameWithHighestRating = null;
  128.                     while (rs.next()) {
  129.                         int rating = rs.getInt(3);
  130.                         if (rating > highestRating) {
  131.                             highestRating = rating;
  132.                             restIdWithHighestRating = rs.getInt(1);
  133.                         }
  134.                     }
  135.                     _query = "SELECT * FROM RESTAURANT WHERE ID=" + restIdWithHighestRating + ";";
  136.                     rs = stmt.executeQuery(_query);
  137.                     restoNameWithHighestRating = rs.getString(2);
  138.  
  139.                     _query = "SELECT * FROM PRODUCT WHERE Product_ID=" + productId + ";";
  140.                     rs = stmt.executeQuery(_query);
  141.                     String productName = rs.getString(2);
  142.  
  143.                     System.out.println();
  144.                     System.out.printf("Product name - Restaurant name - Rating");
  145.                     System.out.println("********************************************");
  146.                     System.out.println(productName + " - " + restoNameWithHighestRating + " - " + highestRating);
  147.                     //System.out.printf("%-16s - %-16s %-2d",productName,restoNameWithHighestRating,highestRating);
  148.                     rs.close();
  149.                     stmt.close();
  150.                     conn.close();
  151.                 } catch (SQLException e) {
  152.                     e.printStackTrace();
  153.                 }
  154.             } else if (choice == 2) {
  155.                 /* TODO */
  156.             } else {
  157.                 /* TODO */
  158.             }
  159.         }
  160.         System.out.println();
  161.         System.out.println("End of Session");
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement