University of Cincinnati logo and link  
Database Bean Source
 
  UC ingot 

/*
 * Database.java
 *
 * Created on April 14, 2003, 11:23 PM
 */

import java.beans.*;
import java.sql.*;

/**
 *
 * @author  Brandan Jones
 */
public class Database extends Object implements java.io.Serializable {
 

    private static final String PROP_DRIVER_NAME = "DriverName";
    private static final String PROP_CONNECTION_URL = "ConnectionURL";
    private static final String PROP_QUERY_STATEMENT = "QueryStatement";
    
    private PropertyChangeSupport propertySupport;

    private String driverName;
    private String connectionURL;
    private String queryStatement;

    java.sql.Connection conn;
        
    /** Creates new Database */
    public Database() {
        propertySupport = new PropertyChangeSupport ( this );
    }

    public String getQueryStatement () {
        return queryStatement;
    }

    public void setQueryStatement(String value) {
        String oldValue = queryStatement;
        queryStatement = value;
        propertySupport.firePropertyChange (PROP_QUERY_STATEMENT, oldValue, queryStatement);
    }
    
    public String getConnectionURL () {
        return connectionURL;
    }

    public void setConnectionURL (String value) {
        String oldValue = connectionURL;
        connectionURL = value;
        propertySupport.firePropertyChange (PROP_CONNECTION_URL, oldValue, connectionURL);
    }
    
      public String getDriverName () {
        return driverName;
    }

    public void setDriverName (String value) {
        String oldValue = driverName;
        driverName = value;
        propertySupport.firePropertyChange (PROP_DRIVER_NAME, oldValue, driverName);
    }

    public void addPropertyChangeListener (PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener (listener);
    }

    public void removePropertyChangeListener (PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener (listener);
    }
    
    // Initialize the varibles based on our properties.
    
    public void initialize() {
        try {
            Class.forName(driverName).newInstance();
            conn = DriverManager.getConnection(connectionURL);
        } catch (Exception e) {
            System.out.println("Exception while attempting to set up database.  Message: " + e.getMessage());
        }
    }
    
    // run some query.
    
    public void runQuery() {
        try {

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(queryStatement);
            while(rs.next()) {
                ResultSetMetaData rsmd = rs.getMetaData();
                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    System.out.print(rs.getString(i+1));
                }
                // new line for new record.
                System.out.println(" ");
            }
        } catch (Exception e) {
            System.out.println("Exception while attempting to make database call.  Message: " + e.getMessage());
            
        }
    }

}
 Screen Captures