Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Generalizing Connection Information - Interactive

 


[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.

    Task 1

    Given the following ConnectJ.properties file:

    #PropertiesResourceBundle for Connection Properties
    CSDriver=COM.cloudscape.core.RmiJdbcDriver
    CSURL=jdbc:cloudscape:rmi:jGuru
    

    Set up String variables to access the keys and to hold the returned values. Also set up Strings 1) containing the name of the properties file - ConnectJ, and 2) containing a SELECT statement to get all columns and rows, but omit the table name, which will be obtained and appended later in the program.

    Insert the following code:

      String sDriver,
             sDriverKey = "CSDriver",
             sPassword,
             sQuery = 
              "SELECT * FROM ",
             srbName = "ConnectJ",
             sTable,
             sURL,
             sURLKey="CSURL",
             sUserID;
    

    Task 2

    Access the ResourceBundle and retrieve the driver name and databaseURL.

          rbConnect = ResourceBundle.getBundle( srbName );
    
          sDriver   = rbConnect.getString( sDriverKey );
          sURL      = rbConnect.getString( sURLKey );
    

    Task 3

    Display a GUI form which requests a userID,password and table name, has a "Connect" button and a textarea. This is done for you. When the button is clicked, in actionPerformed(), verify that a table name was entered ( from jtTable, ) get the userID ( from jtUserID ) and password ( from jpfPassword ) keyed, reset the text area ( jta ) and invoke doConnect().

        sTable = jtTable.getText();
        if( sTable.equals("") )
        {
          jta.setText( "Table must have an entry." );
          return;
        }
        sUserID = jtUserID.getText();
        sPassword = jpfPassword.getText();
        jta.setText( "" );
    
        doConnect();
    

    Task 4

    In doConnect(), the driver is loaded, a Connection obtained and a Statement created as before. Your job: concatenate the query string and table name, then execute the query.

          rs = stmt.executeQuery( sQuery + sTable );
    

    Task 5

    In doConnect(), a boolean, a StringBuffer and a while( rs.next() loop are provided. Inside the loop, an if( bFirst ) section to determine the first time through the loop is set up. Your job: use ResultSetMetadata to get and display ColumnCount number of ColumnLabels in a single String in the text area jta. Reset the StringBuffer and ensure that this process only happens once per while loop.

              rsmd = rs.getMetaData();
              iColumnCount = rsmd.getColumnCount();
              for( int i = 1; i <= iColumnCount; i++ )
              {
                sb.append( 
                   rsmd.getColumnLabel( i ) + "\t" );
              } 
    
              jta.append( sb.toString() + "\n");
              sb.replace( 0, sb.length(), "" );
              bFirst = false;
    

    Task 6

    In doConnect(), on every pass of the while loop, obtain ColumnCount number of columns for the row using ResultSet.getObject() and concatenate these into a single String. getObject(), as used here, will obtain a String from virtually any type returned from the database. Append the resulting String to the text area jta and reset the StringBuffer.

            for( int i = 1; i <= iColumnCount; i++ )
            {
              sb.append( rs.getObject( i ) + "\t" ); 
            } 
    
            jta.append( sb.toString() + "\n");
            sb.replace( 0, sb.length(), "" );
    

Copyright 1996-2000 jGuru.com. All Rights Reserved.