Advertisement
Guest User

Prob 2 Java Code (Option 1 Done)

a guest
Oct 11th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 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.                        
  65.                         break;
  66.                     default:
  67.                         System.out.print("Invalid option, please enter an option from 1 to 4");
  68.                         break;
  69.                
  70.                 }
  71.                 System.out.println("Please select an option by typing an integer");
  72.                 i = sc.nextInt();
  73.             }
  74.             System.out.println("Exiting");
  75.             dbConnection.close();
  76.             sc.close();
  77.         }catch( SQLException x ){
  78.             System.out.println("Couldn’t get connection!");
  79.         }
  80.        
  81.        
  82.        
  83.        
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement