com.rapidminer.tools.jdbc
Class DatabaseHandler

java.lang.Object
  extended by com.rapidminer.tools.jdbc.DatabaseHandler

public class DatabaseHandler
extends java.lang.Object

This class hides the database. Using connect(String,String,boolean), you can extablish a connection to the database. Once connected, queries and updates are possible.

Please note that the queries does not end with the statement terminator (e.g. ; for Oracle or GO for Sybase). The JDBC driver will automatically add the correct terminator.

Author:
Ingo Mierswa

Field Summary
static int OVERWRITE_MODE_APPEND
           
static int OVERWRITE_MODE_NONE
           
static int OVERWRITE_MODE_OVERWRITE
           
static int OVERWRITE_MODE_OVERWRITE_FIRST
           
static java.lang.String[] OVERWRITE_MODES
           
 
Constructor Summary
DatabaseHandler(java.lang.String databaseURL, JDBCProperties properties)
          Constructor of the database handler.
 
Method Summary
 void addColumn(Attribute attribute, java.lang.String tableName)
          Adds a column for the given attribute to the table with name tableName.
 void commit()
          Makes all changes to the database permanent.
 void connect(java.lang.String username, java.lang.String passwd, boolean autoCommit)
          Establishes a connection to the database.
static java.util.List<Attribute> createAttributes(java.sql.ResultSet rs)
          Creates a list of attributes reflecting the result set's column meta data.
 java.sql.PreparedStatement createPreparedStatement(java.lang.String sqlString, boolean scrollableAndUpdatable)
          Create a prepared statement where result sets will have the properties TYPE_SCROLL_SENSITIVE and CONCUR_UPDATABLE.
 java.sql.Statement createStatement(boolean scrollableAndUpdatable)
          Create a statement where result sets will have the properties TYPE_SCROLL_SENSITIVE and CONCUR_UPDATABLE.
 void createTable(ExampleSet exampleSet, java.lang.String tableName, int overwriteMode, boolean firstAttempt, int defaultVarcharLength)
          Creates a new table in this connection and fills it with the provided data.
 void disconnect()
          Closes the connection to the database.
 java.util.Map<java.lang.String,java.util.List<ColumnIdentifier>> getAllTableMetaData()
           
static DatabaseHandler getConnectedDatabaseHandler(java.lang.String databaseURL, java.lang.String username, java.lang.String password, JDBCProperties properties, LoggingHandler logging)
          Returns a connected database handler instance from the given connection data.
 java.sql.Connection getConnection()
          Returns the connection.
static java.lang.String getDatabaseName(Attribute attribute)
          Deprecated. Use the open and close quotes for identifiers from the properties instead
 JDBCProperties getProperties()
          Returns the JDBC properties associated with this handler.
static int getRapidMinerTypeIndex(int sqlType)
          Returns for the given SQL-type the name of the corresponding RapidMiner-Type from com.rapidminer.tools.Ontology.
 java.sql.ResultSet query(java.lang.String sqlQuery)
          Deprecated. Use the method createStatement(boolean) instead and perform the queries explicitely since this method would not allow closing the statement
 void removeColumn(Attribute attribute, java.lang.String tableName)
          Removes the column of the given attribute from the table with name tableName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OVERWRITE_MODES

public static final java.lang.String[] OVERWRITE_MODES

OVERWRITE_MODE_NONE

public static final int OVERWRITE_MODE_NONE
See Also:
Constant Field Values

OVERWRITE_MODE_OVERWRITE_FIRST

public static final int OVERWRITE_MODE_OVERWRITE_FIRST
See Also:
Constant Field Values

OVERWRITE_MODE_OVERWRITE

public static final int OVERWRITE_MODE_OVERWRITE
See Also:
Constant Field Values

OVERWRITE_MODE_APPEND

public static final int OVERWRITE_MODE_APPEND
See Also:
Constant Field Values
Constructor Detail

DatabaseHandler

public DatabaseHandler(java.lang.String databaseURL,
                       JDBCProperties properties)
Constructor of the database handler. This constructor expects the URL definition of the database which is needed by the System DriverManager to create an appropriate driver. Please note that this database handler still must be connected via invoking the method connect(String, String, boolean). If you want to directly use a connected database handler you might use the static method getConnectedDatabaseHandler(String, String, String, JDBCProperties, LoggingHandler) instead.

Method Detail

getConnectedDatabaseHandler

public static DatabaseHandler getConnectedDatabaseHandler(java.lang.String databaseURL,
                                                          java.lang.String username,
                                                          java.lang.String password,
                                                          JDBCProperties properties,
                                                          LoggingHandler logging)
                                                   throws OperatorException,
                                                          java.sql.SQLException
Returns a connected database handler instance from the given connection data. If the password is null, it will be queries by the user during this method.

Throws:
OperatorException
java.sql.SQLException

getProperties

public JDBCProperties getProperties()
Returns the JDBC properties associated with this handler.


connect

public void connect(java.lang.String username,
                    java.lang.String passwd,
                    boolean autoCommit)
             throws java.sql.SQLException
Establishes a connection to the database. Afterwards, queries and updates can be executed using the methods this class provides.

Parameters:
username - Name with which to log in to the database. Might be null.
passwd - Password with which to log in to the database. Might be null.
autoCommit - If TRUE, all changes to the database will be committed automatically. If FALSE, the commit()-Method has to be called to make changes permanent.
Throws:
java.sql.SQLException

disconnect

public void disconnect()
                throws java.sql.SQLException
Closes the connection to the database.

Throws:
java.sql.SQLException

getConnection

public java.sql.Connection getConnection()
Returns the connection. Might be used in order to create statements. The return value might be null if this database handler is not connected.


createStatement

public java.sql.Statement createStatement(boolean scrollableAndUpdatable)
                                   throws java.sql.SQLException
Create a statement where result sets will have the properties TYPE_SCROLL_SENSITIVE and CONCUR_UPDATABLE. This means that the ResultSet is scrollable and also updatable. It will also directly show all changes to the database made by others after this ResultSet was obtained. Will throw an SQLException if the handler is not connected.

Throws:
java.sql.SQLException

createPreparedStatement

public java.sql.PreparedStatement createPreparedStatement(java.lang.String sqlString,
                                                          boolean scrollableAndUpdatable)
                                                   throws java.sql.SQLException
Create a prepared statement where result sets will have the properties TYPE_SCROLL_SENSITIVE and CONCUR_UPDATABLE. This means that the ResultSet is scrollable and also updatable. It will also directly show all changes to the database made by others after this ResultSet was obtained. Will throw an SQLException if the handler is not connected.

Throws:
java.sql.SQLException

commit

public void commit()
            throws java.sql.SQLException
Makes all changes to the database permanent. Invoking this method explicit is usually not necessary if the connection was created with AUTO_COMMIT set to true.

Throws:
java.sql.SQLException

query

@Deprecated
public java.sql.ResultSet query(java.lang.String sqlQuery)
                         throws java.sql.SQLException
Deprecated. Use the method createStatement(boolean) instead and perform the queries explicitely since this method would not allow closing the statement

Executes the given SQL-Query. Only SQL-statements starting with "SELECT" (disregarding case) will be accepted. Any errors will result in an SQL-Exception being thrown.

Parameters:
sqlQuery - An SQL-String.
Returns:
A ResultSet-Object with the results of the query. The ResultSet is scrollable, but not updatable. It will not show changes to the database made by others after this ResultSet was obtained.
Throws:
java.sql.SQLException

addColumn

public void addColumn(Attribute attribute,
                      java.lang.String tableName)
               throws java.sql.SQLException
Adds a column for the given attribute to the table with name tableName.

Throws:
java.sql.SQLException

removeColumn

public void removeColumn(Attribute attribute,
                         java.lang.String tableName)
                  throws java.sql.SQLException
Removes the column of the given attribute from the table with name tableName.

Throws:
java.sql.SQLException

createTable

public void createTable(ExampleSet exampleSet,
                        java.lang.String tableName,
                        int overwriteMode,
                        boolean firstAttempt,
                        int defaultVarcharLength)
                 throws java.sql.SQLException
Creates a new table in this connection and fills it with the provided data.

Throws:
java.sql.SQLException - if the table should be overwritten but a table with this name already exists

getRapidMinerTypeIndex

public static int getRapidMinerTypeIndex(int sqlType)
Returns for the given SQL-type the name of the corresponding RapidMiner-Type from com.rapidminer.tools.Ontology.


createAttributes

public static java.util.List<Attribute> createAttributes(java.sql.ResultSet rs)
                                                  throws java.sql.SQLException
Creates a list of attributes reflecting the result set's column meta data.

Throws:
java.sql.SQLException

getDatabaseName

@Deprecated
public static java.lang.String getDatabaseName(Attribute attribute)
Deprecated. Use the open and close quotes for identifiers from the properties instead


getAllTableMetaData

public java.util.Map<java.lang.String,java.util.List<ColumnIdentifier>> getAllTableMetaData()
                                                                                     throws java.sql.SQLException
Throws:
java.sql.SQLException


Copyright © 2001-2009 by Rapid-I