[ Back | Previous | Next ]

A JdbcAdapter Bean

Package:
java.sql.*
Product:
JDK
Release:
1.1.x
Related Links:
General
General
General
General
CallableStatement
Comment:
A universal JdbcAdapter to connect to databases. Using jdbc by configuration of the adapter as follows:
         jrc = new nl.rotterdam.port.database.JdbcAdapter();
         jrc.setPassword("");
         jrc.setConnectString("jdbc:odbc:Northwind");
         jrc.setJdbcRecord(null);
         jrc.setUsername("admin");
         jrc.setDriverClass("sun.jdbc.odbc.JdbcOdbcDriver");
         results = new List(10);
The source for the JdbcAdapter:

package nl.rotterdam.port.database;

import java.sql.*;
/**
 * This class was generated by a SmartGuide.
 * 
 */
public class JdbcAdapter {
   public Connection con = null;
   public static String q = null;
   String fieldConnectString = "";
   String fieldDriverClass = "";
   JdbcRecord fieldJdbcRecord = null;
   String fieldPassword = "";
   String fieldUsername = "";
   protected transient java.beans.PropertyChangeSupport propertyChange = new java.beans.PropertyChangeSupport(this);

/**
 * Constructor
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
public JdbcAdapter() {
   super();
   initialize();
}

/**
 * The addPropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
   propertyChange.addPropertyChangeListener(listener);
}
/**
 * Performs the close method.
 */
public void close() throws java.sql.SQLException {
   /* Perform the close method. */
   con.close();
   return;
}
/**
 * The firePropertyChange method was generated to support the propertyChange field.
 */
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
   propertyChange.firePropertyChange(propertyName, oldValue, newValue);
}
/**
 * Gets the connectString property (java.lang.String) value.
 * @return The connectString property value.
 * @see #setConnectString
 */
public String getConnectString() {
   /* Returns the connectString property value. */
   if (fieldConnectString == null) {
      try {
         fieldConnectString = new String();
      } catch (Throwable exception) {
         System.err.println("Exception creating connectString property.");
      }
   };
   return fieldConnectString;
}
/**
 * Gets the driverClass property (java.lang.String) value.
 * @return The driverClass property value.
 * @see #setDriverClass
 */
public String getDriverClass() {
   /* Returns the driverClass property value. */
   if (fieldDriverClass == null) {
      try {
         fieldDriverClass = new String();
      } catch (Throwable exception) {
         System.err.println("Exception creating driverClass property.");
      }
   };
   return fieldDriverClass;
}
/**
 * Gets the jdbcRecord property (nl.rotterdam.port.database.JdbcRecord) value.
 * @return The jdbcRecord property value.
 * @see #setJdbcRecord
 */
public JdbcRecord getJdbcRecord() {
   /* Returns the jdbcRecord property value. */
   return fieldJdbcRecord;
}
/**
 * Gets the password property (java.lang.String) value.
 * @return The password property value.
 * @see #setPassword
 */
public String getPassword() {
   /* Returns the password property value. */
   if (fieldPassword == null) {
      try {
         fieldPassword = new String();
      } catch (Throwable exception) {
         System.err.println("Exception creating password property.");
      }
   };
   return fieldPassword;
}
/**
 * Gets the username property (java.lang.String) value.
 * @return The username property value.
 * @see #setUsername
 */
public String getUsername() {
   /* Returns the username property value. */
   if (fieldUsername == null) {
      try {
         fieldUsername = new String();
      } catch (Throwable exception) {
         System.err.println("Exception creating username property.");
      }
   };
   return fieldUsername;
}
/**
 * Called whenever the part throws an exception.
 * @param exception java.lang.Throwable
 */
private void handleException(Throwable exception) {

   /* Uncomment the following lines to print uncaught exceptions to stdout */
   // System.out.println("--------- UNCAUGHT EXCEPTION ---------");
   // exception.printStackTrace(System.out);
}

/**
 * Initialize class
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
   // user code begin {1}
   // user code end
   // user code begin {2}
   // user code end
}

/**
 * main entrypoint - starts the part when it is run as an application
 * @param args java.lang.String[]
 */
public static void main(java.lang.String[] args) {
   try {
      nl.rotterdam.port.database.JdbcAdapter aJdbcAdapter = new nl.rotterdam.port.database.JdbcAdapter();
   } catch (Throwable exception) {
      System.err.println("Exception occurred in main() of java.lang.Object");
   }
}

/**
 * Performs the open method.
 */
public void open() throws java.sql.SQLException {
   /* Perform the open method. */
   try {
        Class.forName ( getDriverClass() );
        System.out.println("Loading Oracle driver.. " + getDriverClass() );
     } catch( ClassNotFoundException ee) {   
        ee.printStackTrace();
     }

 /*  // Set Security Rights for the Netscape Browser 
   try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalConnect");
      System.out.println("Enabling Netscape Security: 'UnviversalConnect'.");
   } catch (Exception ne) {
      System.out.println("No Netscape Security features available.." + ne.toString());
   } catch (Error e) {
      System.out.println("No Netscape Security features available.." + e.toString() );
   }         
*/
   // Connect to the database 
   con = DriverManager.getConnection (getConnectString(), getUsername(), getPassword() );
        
   if ( con == null ) {
      System.err.println("Unable to get Database connection");
   }   
   else {
      System.out.println("connected to database " + getConnectString() );
   }   

   return;
}
/**
 * The removePropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
   propertyChange.removePropertyChangeListener(listener);
}
/**
 * Performs the select method.
 * @param arg1 java.lang.String
 */
public void select(String query) {
   /* Perform the select method. */
   try {
      // Open connection to database
      open();

      // check for connectivity
      if (con == null ) {
         throw new java.net.NoRouteToHostException("No connection to database...");
      }   
            
      // Create a SQL Statement
      System.out.println("getting connection: " + con);
      Statement stmt = con.createStatement ();
      System.out.println("Creating SQL Statement:  " + stmt);
      //stmt.setMaxRows(100);
      //stmt.setQueryTimeout(17);
      // Execute the query
      System.out.println("Executing query " + query + "\n");
      //ResultSet rs = stmt.executeQuery ( "SELECT * FROM telfgids.gebruiker WHERE gbr_achternaam like '%aa%'" );
      //q = new String(query);
      ResultSet rs = stmt.executeQuery( query );
      
         
      // Check to see if any rows were read
      // Loop through the rows retrieved from the query
      // Print out the first column 
      System.out.println("results:" + rs.next());
      System.out.println("results:" + rs);
      System.out.println("Starting to find results.\n\n\n");
      while (rs.next ()) {
           // adding record by invoking the interface to our application if jr interface exists
           if ( getJdbcRecord()!=null ) {
            try {
                 getJdbcRecord().addRecord( rs );
            } catch (java.rmi.RemoteException re) {
               System.err.println( "remote exception:" + re );
            }
         } else {
            System.out.println("" +
            rs.getString(1) + ": " +
            rs.getString(2) + ": " );
            System.out.print("" + 
            rs.getString(3) + ": "  );
         }   
         //System.out.println("results: " +   rs.getString(1) );
         
         
      } 
      System.out.println("\n\n\nEnding to find results.");
      System.out.println("Warnings: " + stmt.getWarnings() );
      rs.close(); 
      stmt.close();
      close();
      
   } catch (SQLException re) {
   } catch (java.net.NoRouteToHostException nrth) {
      System.err.println( "" + nrth.toString() + ": " + nrth.getMessage() );
   } catch (Exception e) {
      System.out.println( "" + e.toString() + ": " + e.getMessage() );
   } finally {
      if ( con != null) {
         try {
               close();
         } catch (Exception e) {
            System.err.println("Not able to close database connection.");
            e.printStackTrace();
         }   
      }   
   }   
   System.err.println("exiting select() ");
   return;
}
/**
 * Sets the connectString property (java.lang.String) value.
 * @param connectString The new value for the property.
 * @see #getConnectString
 */
public void setConnectString(String connectString) {
   /* Get the old property value for fire property change event. */
   String oldValue = fieldConnectString;
   /* Set the connectString property (attribute) to the new value. */
   fieldConnectString = connectString;
   /* Fire (signal/notify) the connectString property change event. */
   firePropertyChange("connectString", oldValue, connectString);
   return;
}
/**
 * Sets the driverClass property (java.lang.String) value.
 * @param driverClass The new value for the property.
 * @see #getDriverClass
 */
public void setDriverClass(String driverClass) {
   /* Get the old property value for fire property change event. */
   String oldValue = fieldDriverClass;
   /* Set the driverClass property (attribute) to the new value. */
   fieldDriverClass = driverClass;
   /* Fire (signal/notify) the driverClass property change event. */
   firePropertyChange("driverClass", oldValue, driverClass);
   return;
}
/**
 * Sets the jdbcRecord property (nl.rotterdam.port.database.JdbcRecord) value.
 * @param jdbcRecord The new value for the property.
 * @see #getJdbcRecord
 */
public void setJdbcRecord(JdbcRecord jdbcRecord) {
   /* Get the old property value for fire property change event. */
   JdbcRecord oldValue = fieldJdbcRecord;
   /* Set the jdbcRecord property (attribute) to the new value. */
   fieldJdbcRecord = jdbcRecord;
   /* Fire (signal/notify) the jdbcRecord property change event. */
   firePropertyChange("jdbcRecord", oldValue, jdbcRecord);
   return;
}
/**
 * Sets the password property (java.lang.String) value.
 * @param password The new value for the property.
 * @see #getPassword
 */
public void setPassword(String password) {
   /* Get the old property value for fire property change event. */
   String oldValue = fieldPassword;
   /* Set the password property (attribute) to the new value. */
   fieldPassword = password;
   /* Fire (signal/notify) the password property change event. */
   firePropertyChange("password", oldValue, password);
   return;
}
/**
 * Sets the username property (java.lang.String) value.
 * @param username The new value for the property.
 * @see #getUsername
 */
public void setUsername(String username) {
   /* Get the old property value for fire property change event. */
   String oldValue = fieldUsername;
   /* Set the username property (attribute) to the new value. */
   fieldUsername = username;
   /* Fire (signal/notify) the username property change event. */
   firePropertyChange("username", oldValue, username);
   return;
}
/**
 * Returns a String that represents the value of this object.
 */
public String toString() {
   // insert code to print the receiver here
   // this implementation simply forwards the message to super.  You may replace or supplement this.
   return super.toString();
}
}
1