Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.SQL.DriverManager; // DriverManager FOR establishing linkage TO DB
- import java.SQL.SQLException; // SQLException handles exception FOR DB classes
- import java.SQL.Connection; // Connection establishes a logon TO the DB
- import java.SQL.Statement; // Holds AND executes the SQL statements
- import java.SQL.ResultSet; // Holds the results OF a query
- import java.util.DATE;
- public class Hw6Pr1
- {
- /**
- * Method: main<br>
- * Responsibilities: runs sql queries and lists them in the way needed.
- *
- * @param args
- */
- public static void main(String[] args)
- {
- System.OUT.println("Starting Connection Process");
- String myConnStr; // holds the connection string
- String myUser; // holds the USER name
- String myPasswd; // holds the USER password
- String mySqlStmt; // holds SQL statement TO be run
- INT COUNT = 0;// holds the COUNT OF ROWS retrieved
- /*
- * try/catch to handling the exceptions
- */
- try
- {
- //Link the class TO the jdbc driver
- System.OUT.println("Link this class to the jdbc driver");
- DriverManager.registerDriver(NEW oracle.jdbc.OracleDriver());
- //connection TO the DATABASE
- myConnStr = "jdbc:oracle:thin:@pythia.etsu.edu:1521:csdb";
- myUser = "zldp3";
- myPasswd = "dbms";
- Connection myCon = NULL; //Build the connection object
- myCon = DriverManager.getConnection(myConnStr, myUser, myPasswd); //establish a connection via the jdbc driver
- Statement myStmt = NULL; //holds the VALUE OF the SQL statement
- myStmt = myCon.createStatement(); // the connection association
- Statement statement = NULL;
- statement = myCon.createStatement();
- ResultSet myReslts = NULL; // the RESULT SET
- 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
- myReslts = myStmt.executeQuery(mySqlStmt);
- // tell the jdbc connection TO run the query
- // AND put the results IN my_results
- //looping through the results TO EXTRACT the DATA needed
- while (myReslts.NEXT()) //ResultSet.NEXT gets the NEXT record,
- //it RETURNS FALSE IF no more records exist.
- {
- String givenName = myReslts.getString("GIVEN_NAME"); //holds the VALUE OF the department name
- String familyName = myReslts.getString("FAMILY_NAME"); //holds the VALUE OF the DATE the department was founded
- //counter TO see the POSITION OF the loop
- COUNT = COUNT + 1;
- //Now USE ALL these pieces TO build up a nice output.
- System.OUT.println
- ("Instructor" + COUNT + ": " + givenName + " " + familyName);
- } //END while
- myReslts.close(); // close the RESULT SET
- myStmt.close(); // close the statement
- myCon.close(); // close the connection
- } // END try
- catch (SQLException mySqlError)
- {
- //error handling
- System.OUT.println("An Error has occured, dumping error stack");
- mySqlError.printStackTrace();
- } //END catch
- } // END OF main
- } // END OF class Hw6Pr1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement