Advertisement
Guest User

Current java code (just needs option 2 code)

a guest
Oct 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package hw3;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4.  
  5. public class GP3_Problem2_Group29 {
  6.  
  7.     public static void main(String[] args) {
  8.         //load a database driver
  9.         try {
  10.             Class.forName("oracle.jdbc.OracleDriver");
  11.         }catch(Exception x){
  12.             System.out.println("Unable to load the driver class!");
  13.         }
  14.        
  15.         System.out.println("loading database connection . . .");
  16.         //create an oracle JDBC connection
  17.         try{
  18.             Connection dbConnection=DriverManager.getConnection("jdbc:oracle:thin:@//oracle.cs.ou.edu:1521/pdborcl.cs.ou.edu","bask4499","LJnd8Dz9");
  19.             //create an oracle statement object
  20.             Statement stmt = dbConnection.createStatement();
  21.             System.out.println("Please select an option by typing an integer");
  22.             Scanner sc = new Scanner(System.in);
  23.             int i = sc.nextInt();
  24.             while(i!=4){
  25.                 switch(i){
  26.                     case 1:
  27.                         //OPTION 1
  28.                         //Create new customer record
  29.                         //with user entered cid, cname, level
  30.                         //and number_of_orders = avg num orders for current entries with same level
  31.                         String cid,cname,level;
  32.                         int number_of_orders = 0;
  33.                         System.out.println("cid? ");
  34.                         cid = sc.next();
  35.                         System.out.println("cname? ");
  36.                         cname = sc.next();
  37.                         System.out.println("level? ");
  38.                         level = sc.next();
  39.                        
  40.                         //get the avg num orders for the given level
  41.                         CallableStatement cs;
  42.                         cs = dbConnection.prepareCall("{call GETAVGNUMORDERSFOR(?,?)}");
  43.                         //if parameter is an IN parameter, use cs.set[parameterType](index,value)
  44.                         cs.setString(1, level);
  45.                         //if parameter is an OUT parameter use cs.registerOutParameter,
  46.                         //then cs.get[parameterType] after calling cs.execute
  47.                         cs.registerOutParameter(2, Types.NUMERIC);
  48.                         cs.execute();
  49.                         number_of_orders =  cs.getInt(2);
  50.                        
  51.                         cs = dbConnection.prepareCall("{call NEWCUSTOMER(?,?,?,?)}");
  52.                         cs.setInt(1, Integer.valueOf(cid));
  53.                         cs.setString(2,cname);
  54.                         cs.setInt(3, number_of_orders);
  55.                         cs.setString(4, level);
  56.                         cs.execute();
  57.                         System.out.println("Successfully added new customer record");
  58.                         break;
  59.                     case 2:
  60.                         //TODO: put option 2 code here
  61.                         break;
  62.                     case 3:
  63.                         //display complete information of all customers and translators
  64.                         ResultSet rset = stmt.executeQuery("select * from customer");
  65.                         System.out.println("CID     CNAME     NUMBER OF ORDERS        Level");
  66.                         while (rset.next()){
  67.                             System.out.println(rset.getString(1) + "     " + rset.getString(2) +
  68.                                     "             " + rset.getString(3) +  "               " + rset.getString(4));
  69.                         }
  70.                         rset = stmt.executeQuery("select * from translator");
  71.                         System.out.println("TID     TNAME        SALARY");
  72.                         while (rset.next()){
  73.                             System.out.println(rset.getString(1) + "     " + rset.getString(2) +
  74.                                     "             " + rset.getString(3));
  75.                         }
  76.                         break;
  77.                     default:
  78.                         System.out.print("Invalid option, please enter an option from 1 to 4");
  79.                         break;
  80.                
  81.                 }
  82.                 System.out.println("Please select an option by typing an integer");
  83.                 i = sc.nextInt();
  84.             }
  85.             System.out.println("Exiting");
  86.             dbConnection.close();
  87.             sc.close();
  88.         }catch( SQLException x ){
  89.             System.out.println("Couldn’t get connection!");
  90.         }
  91.        
  92.        
  93.        
  94.        
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement