Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.55 KB | None | 0 0
  1. import java.SQL.DriverManager;   // DriverManager FOR establishing linkage TO DB
  2. import java.SQL.SQLException;    // SQLException handles exception FOR DB classes
  3. import java.SQL.Connection;      // Connection establishes a logon TO the DB
  4. import java.SQL.Statement;       // Holds AND executes the SQL statements
  5. import java.SQL.ResultSet;       // Holds the results OF a query
  6. import java.util.DATE;
  7.  
  8. public class Hw6Pr1
  9. {
  10.  
  11.    /**
  12.     * Method: main<br>
  13.     * Responsibilities: runs sql queries and lists them in the way needed.
  14.     *
  15.  * @param args
  16.  */
  17. public static void main(String[] args)
  18.    {
  19.        System.OUT.println("Starting Connection Process");
  20.        String  myConnStr;   // holds the connection string
  21.        String  myUser;      // holds the USER name
  22.        String  myPasswd;    // holds the USER password
  23.        String  mySqlStmt;   // holds SQL statement TO be run
  24.        INT COUNT = 0;// holds the COUNT OF ROWS retrieved
  25.      
  26.        /*
  27.        * try/catch to handling the exceptions
  28.        */
  29.       try
  30.       {
  31.          //Link the class TO the jdbc driver
  32.  
  33.          System.OUT.println("Link this class to the jdbc driver");
  34.          DriverManager.registerDriver(NEW oracle.jdbc.OracleDriver());
  35.          
  36.          //connection TO the DATABASE
  37.          myConnStr = "jdbc:oracle:thin:@pythia.etsu.edu:1521:csdb";
  38.          myUser = "zldp3";
  39.          myPasswd = "dbms";
  40.          Connection myCon = NULL;     //Build the connection object
  41.          myCon = DriverManager.getConnection(myConnStr, myUser, myPasswd); //establish a connection via the jdbc driver
  42.                                  
  43.          
  44.          Statement myStmt = NULL;          //holds the VALUE OF the SQL statement
  45.          myStmt = myCon.createStatement(); // the connection association
  46.          Statement statement = NULL;
  47.          statement = myCon.createStatement();
  48.          
  49.          ResultSet myReslts = NULL;    // the RESULT SET
  50.          mySqlStmt = "SELECT INSTRUCTOR.GIVEN_NAME, INSTRUCTOR.FAMILY_NAME, SECTION.COURSE, SECTION.TERM FROM INSTRUCTOR    JOIN SECTION ON INSTRUCTOR.ID = SECTION.INSTRUCTOR;"; // the SQL statement TO run
  51.  
  52.          
  53.          
  54.          myReslts = myStmt.executeQuery(mySqlStmt);  
  55.                                     // tell the jdbc connection TO run the query
  56.                                     // AND put the results IN my_results                                        
  57.  
  58.            
  59.          //looping through the results TO EXTRACT the DATA needed
  60.          while (myReslts.NEXT())  //ResultSet.NEXT gets the NEXT record,
  61.                                   //it RETURNS FALSE IF no more records exist.
  62.          {
  63.  
  64.             String givenName =  myReslts.getString("GIVEN_NAME");  //holds the VALUE OF the department name            
  65.             String familyName = myReslts.getString("FAMILY_NAME"); //holds the VALUE OF the DATE the department was founded
  66.  
  67.             //counter TO see the POSITION OF the loop
  68.             COUNT = COUNT + 1;
  69.  
  70.            
  71.             //Now USE ALL these pieces TO build up a nice output.
  72.             System.OUT.println
  73.               ("Instructor" + COUNT + ": " + givenName + " " + familyName);
  74.            
  75.          }  //END while
  76.    
  77.          myReslts.close();   // close the RESULT SET
  78.          myStmt.close();     // close the statement
  79.          myCon.close();      // close the connection
  80.  
  81.       }  // END try
  82.       catch (SQLException mySqlError)
  83.       {
  84.          //error handling
  85.          System.OUT.println("An Error has occured, dumping error stack");
  86.          mySqlError.printStackTrace();
  87.       }  //END catch
  88.    } // END OF main
  89. } // END OF class Hw6Pr1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement