Testing the Connection and the Query Methods

In the following steps, you create a simple Java class to test the methods in the DataHandler.java class. To test your application at this stage, you can temporarily set the value of the jdbcUrl variable to the connection string for your database and set the values of the userid and password variables to the values required to access the HR schema.

  1. Open the DataHandler.java class in the Java Visual Editor from the Application Navigator.
  2. Change the jdbcUrl, userid and password variables to contain the values required for the HR schema as follows:
    String jdbcUrl = "connect-string";
    String userid = "HR";
    String password = "hr";
    

    where connect-string is, for example:

    jdbc:oracle:thin:@localhost:1521:ORACLE
    
  3. Create a new Java class named JavaClient in the hr package. Make it a public class and generate a default constructor and a main method. Note that you must select the Main Method check box from the Optional Attributes panel to generate the main method.

    The skeleton JavaClient.java class is created and displayed in the Java Source Editor.

    See Also:

    Creating a Java Class in JDeveloper in Connecting to Oracle Database 12c Release 1 (12.1) for information about creating a Java class file

  4. Import the ResultSet package:
    import java.sql.ResultSet;
    
  5. In the main method declaration, add exception handling as follows:
    public static void main(String[] args) throws Exception{
    
  6. Replace the JavaClient object created by default with a DataHandler object. Locate the following line in the main method:
    JavaClient javaClient = new JavaClient();
    

    Replace this with:

    DataHandler datahandler = new DataHandler();
    
  7. Define a ResultSet object to hold the results of the getAllEmployees query, and iterate through the rows of the result set, displaying the first four columns, Employee Id, First Name, Last Name, and Email. To do this, add the following code to the main method:
    ResultSet rset = datahandler.getAllEmployees();
    
    while (rset.next()) {
    System.out.println(rset.getInt(1) + " " +
      rset.getString(2) + " " + 
      rset.getString(3) + " " + 
      rset.getString(4));
    }
    
  8. Compile the JavaClient.java file to check for compilation errors. To do this, right-click in the Java Source Editor, and select Make from the shortcut menu.

    If there are no errors in compilation, you should see the following message in the Log window:

    Successful compilation: 0 errors, 0 warnings

  9. Run the JavaClient.java file. To do this, right-click in the Java Source Editor window and select Run from the shortcut menu.
  10. Examine the output in the Log window. Notice the trace message, followed by the four columns from the Employees table as shown in Figure 4-1.

    Figure 4-1 Test Output for Query Method in Log Window

    Description of Figure 4-1 follows
    Description of "Figure 4-1 Test Output for Query Method in Log Window"
  11. When you finish testing the application, set the jdbcUrl, userid and password variables in DataHandler.java back to null.