Guest User

Untitled

a guest
Apr 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. /* IP Adress:       134.58.95.246
  5.  * Port:            1521
  6.  * Username:        student
  7.  * Password:        student
  8.  */
  9.  
  10. public class Main {
  11.    
  12.     static {
  13.         try {
  14.             /* Type 4 Driver */
  15.             Class.forName("oracle.jdbc.OracleDriver");
  16.         }
  17.         catch (ClassNotFoundException e) {
  18.             System.err.println("Could not load Oracle 10g driver \n");
  19.             System.err.println(e.getMessage());
  20.             System.exit(1);
  21.         }
  22.     }
  23.    
  24.     public static String askDiscipline() {
  25.        
  26.         String discipline = null;
  27.         try
  28.         {
  29.         BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  30.         System.out.print("Enter a discipline: ");
  31.         discipline = br1.readLine();               
  32.         }
  33.         catch (IOException e)
  34.         {
  35.         System.out.print("Failed to get discipline");
  36.         System.out.println(":" + e.getMessage());
  37.         System.exit(1);
  38.         }
  39.         return discipline;
  40.     }
  41.    
  42.     public static void main(String[] args) {
  43.        
  44.         /* Location of the database */
  45.         String url = "jdbc:oracle:thin:@134.58.95.246:1521:XE";
  46.        
  47.        
  48.         /* Connect to Oracle database */
  49.         Connection conn = null;
  50.         try {
  51.             conn = DriverManager.getConnection(url, "student", "student");
  52.             System.out.println("Connection established...");
  53.             System.out.println();
  54.         } catch (SQLException e1) {
  55.             e1.printStackTrace();
  56.         }
  57.        
  58.        
  59.         /* Initial input */
  60.         String discipline = askDiscipline();
  61.         /* Start loop that asks for input, query, and displays result */
  62.         while (!discipline.equals("0")) {           // => geeft fout: The local variable discipline may not have been initialized
  63.             try {
  64.                
  65.                 /* Query */
  66.                 String query =  "SELECT LASTNAME, FIRSTNAME, RESULT " +
  67.                         "FROM ATHLETE.ATHLETE, ATHLETE.DISCIPLINE, ATHLETE.RESULT " +
  68.                         "WHERE ATHLETE.ANR = RESULT.ANR " +
  69.                         "AND DISCIPLINE.DNR = RESULT.DNR " +
  70.                         "AND DISCIPLINE.DNAME = " + discipline; // + " " + 
  71.                         //"ORDER BY RESULT DESC LIMIT 0,3 ";
  72.                 /* Create statement */
  73.                
  74.                 System.out.println("De query is:\n" + query);
  75.                
  76.                 Statement stmt = conn.createStatement();
  77.                 /* Execute the query */
  78.                 ResultSet rs = stmt.executeQuery(query);
  79.                 /* Output */
  80.                 System.out.println("First name // Last name // Result");
  81.                 System.out.println("-------------------------------");
  82.                 while (rs.next()) {
  83.                     System.out.print(rs.getString(1));
  84.                     System.out.print((" // "));
  85.                     System.out.print(rs.getString(2));
  86.                     System.out.print((" // "));
  87.                     System.out.println(rs.getString(3));
  88.                 }
  89.                 rs.close();
  90.                 stmt.close();
  91.                 conn.close();
  92.             }
  93.             catch (SQLException e) {
  94.                 System.out.println("SQL Exception: ");
  95.                 System.err.println(e.getMessage());
  96.             }
  97.             discipline = askDiscipline();
  98.         }
  99.        
  100.         System.out.println("End Of Session");      
  101.     }
  102. }
Add Comment
Please, Sign In to add comment