weka.core.converters
Class DatabaseConnection

java.lang.Object
  extended by weka.core.converters.DatabaseConnection
All Implemented Interfaces:
java.io.Serializable

public class DatabaseConnection
extends java.lang.Object
implements java.io.Serializable

Connects to a database. WARNING:This class was taken from the weka_src.jar and modified in order to put some logs and to make possible the connection to the DB through SpagoBI

Version:
$Revision: 1.2 $
Author:
Len Trigg (trigg@cs.waikato.ac.nz), Stefan Mutter (mutter@cs.waikato.ac.nz)
See Also:
Serialized Form

Field Summary
static int BOOL
           
static int BYTE
           
protected  java.sql.Connection connection
          The database connection
static int DATE
           
static int DOUBLE
           
protected static java.util.Vector DRIVERS
          Holds the jdbc drivers to be used (only to stop them being gc'ed)
static int FLOAT
           
static int INTEGER
           
static int LONG
           
protected  boolean m_checkForUpperCaseNames
           
protected  boolean m_createIndex
           
protected  java.lang.String m_DatabaseURL
          Database URL
protected  boolean m_Debug
          True if debugging output should be printed
protected  java.lang.String m_password
          Database Password
protected  java.sql.PreparedStatement m_PreparedStatement
          The prepared statement used for database queries.
protected  boolean m_setAutoCommit
           
protected  java.lang.String m_userName
          Database username
protected static java.util.Properties PROPERTIES
          Properties associated with the database connection
protected static java.lang.String PROPERTY_FILE
          The name of the properties file
static int SHORT
           
static int STRING
           
 
Constructor Summary
DatabaseConnection()
          Sets up the database drivers.
 
Method Summary
static java.lang.String arrayToString(java.lang.Object[] array)
          Converts an array of objects to a string by inserting a space between each element.
 void connectToDatabase()
          Opens a connection to the database.
 java.lang.String databaseURLTipText()
          Returns the tip text for this property.
 void disconnectFromDatabase()
          Closes the connection to the database.
 boolean execute(java.lang.String query)
          Executes a SQL query.
 boolean fastExecute(java.lang.String query)
          Fast execute.
 java.lang.String getDatabaseURL()
          Get the value of DatabaseURL.
 java.sql.DatabaseMetaData getMetaData()
          Gets meta data for the database connection object.
 java.lang.String getPassword()
          Get the database password.
 java.sql.ResultSet getResultSet()
          Gets the results generated by a previous query.
 int getUpdateCount()
          Dewtermines if the current query retrieves a result set or updates a table.
 boolean getUpperCase()
          Check if the property checkUpperCaseNames in the DatabaseUtils file is set to true or false.
 java.lang.String getUsername()
          Get the database username.
 boolean isConnected()
          Returns true if a database connection is active.
 boolean isConnectionMenagedByExternalPool()
          Checks if is connection menaged by external pool.
 boolean isTableEmpty(java.lang.String tableName)
          Checks that a given table is empty.
 void setConnection(java.sql.Connection connection)
          Sets the connection.
 void setConnectionMenagedByExternalPool(boolean connectionMenagedByExternalPool)
          Sets the connection menaged by external pool.
 void setDatabaseURL(java.lang.String newDatabaseURL)
          Set the value of DatabaseURL.
 void setPassword(java.lang.String password)
          Set the database password.
 void setUsername(java.lang.String username)
          Set the database username.
 boolean tableExists(java.lang.String tableName)
          Checks that a given table exists.
static java.lang.String typeName(int type)
          Returns the name associated with a SQL type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_FILE

protected static java.lang.String PROPERTY_FILE
The name of the properties file


DRIVERS

protected static java.util.Vector DRIVERS
Holds the jdbc drivers to be used (only to stop them being gc'ed)


PROPERTIES

protected static java.util.Properties PROPERTIES
Properties associated with the database connection


m_DatabaseURL

protected java.lang.String m_DatabaseURL
Database URL


m_PreparedStatement

protected java.sql.PreparedStatement m_PreparedStatement
The prepared statement used for database queries.


connection

protected java.sql.Connection connection
The database connection


m_Debug

protected boolean m_Debug
True if debugging output should be printed


m_checkForUpperCaseNames

protected boolean m_checkForUpperCaseNames

m_setAutoCommit

protected boolean m_setAutoCommit

m_createIndex

protected boolean m_createIndex

m_userName

protected java.lang.String m_userName
Database username


m_password

protected java.lang.String m_password
Database Password


STRING

public static final int STRING
See Also:
Constant Field Values

BOOL

public static final int BOOL
See Also:
Constant Field Values

DOUBLE

public static final int DOUBLE
See Also:
Constant Field Values

BYTE

public static final int BYTE
See Also:
Constant Field Values

SHORT

public static final int SHORT
See Also:
Constant Field Values

INTEGER

public static final int INTEGER
See Also:
Constant Field Values

LONG

public static final int LONG
See Also:
Constant Field Values

FLOAT

public static final int FLOAT
See Also:
Constant Field Values

DATE

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

DatabaseConnection

public DatabaseConnection()
                   throws java.lang.Exception
Sets up the database drivers.

Throws:
java.lang.Exception - the exception
java.lang.Exception - if an error occurs
Method Detail

setUsername

public void setUsername(java.lang.String username)
Set the database username.

Parameters:
username - Username for Database.

getUsername

public java.lang.String getUsername()
Get the database username.

Returns:
Database username

setPassword

public void setPassword(java.lang.String password)
Set the database password.

Parameters:
password - Password for Database.

getPassword

public java.lang.String getPassword()
Get the database password.

Returns:
Password for Database.

databaseURLTipText

public java.lang.String databaseURLTipText()
Returns the tip text for this property.

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getDatabaseURL

public java.lang.String getDatabaseURL()
Get the value of DatabaseURL.

Returns:
Value of DatabaseURL.

setDatabaseURL

public void setDatabaseURL(java.lang.String newDatabaseURL)
Set the value of DatabaseURL.

Parameters:
newDatabaseURL - Value to assign to DatabaseURL.

getUpperCase

public boolean getUpperCase()
Check if the property checkUpperCaseNames in the DatabaseUtils file is set to true or false.

Returns:
true if the property checkUpperCaseNames in the DatabaseUtils file is set to true, false otherwise.

arrayToString

public static java.lang.String arrayToString(java.lang.Object[] array)
Converts an array of objects to a string by inserting a space between each element. Null elements are printed as ?

Parameters:
array - the array of objects
Returns:
a value of type 'String'

typeName

public static java.lang.String typeName(int type)
Returns the name associated with a SQL type.

Parameters:
type - the SQL type
Returns:
the name of the type

connectToDatabase

public void connectToDatabase()
                       throws java.lang.Exception
Opens a connection to the database.

Throws:
java.lang.Exception - the exception
java.lang.Exception - if an error occurs

disconnectFromDatabase

public void disconnectFromDatabase()
                            throws java.lang.Exception
Closes the connection to the database.

Throws:
java.lang.Exception - the exception
java.lang.Exception - if an error occurs

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.lang.Exception
Gets meta data for the database connection object.

Returns:
the meta data.
Throws:
java.lang.Exception - the exception
java.sql.SQLException - if an error occurs

isConnected

public boolean isConnected()
Returns true if a database connection is active.

Returns:
a value of type 'boolean'

execute

public boolean execute(java.lang.String query)
                throws java.sql.SQLException
Executes a SQL query.

Parameters:
query - the SQL query
Returns:
true if the query generated results
Throws:
java.sql.SQLException - the SQL exception
java.sql.SQLException - if an error occurs

fastExecute

public boolean fastExecute(java.lang.String query)
                    throws java.sql.SQLException
Fast execute.

Parameters:
query - the query
Returns:
true, if successful
Throws:
java.sql.SQLException - the SQL exception

getResultSet

public java.sql.ResultSet getResultSet()
                                throws java.sql.SQLException
Gets the results generated by a previous query.

Returns:
the result set.
Throws:
java.sql.SQLException - the SQL exception
java.sql.SQLException - if an error occurs

getUpdateCount

public int getUpdateCount()
                   throws java.sql.SQLException
Dewtermines if the current query retrieves a result set or updates a table.

Returns:
the update count (-1 if the query retrieves a result set).
Throws:
java.sql.SQLException - the SQL exception
java.sql.SQLException - if an error occurs

tableExists

public boolean tableExists(java.lang.String tableName)
                    throws java.lang.Exception
Checks that a given table exists.

Parameters:
tableName - the name of the table to look for.
Returns:
true if the table exists.
Throws:
java.lang.Exception - the exception
java.lang.Exception - if an error occurs.

isTableEmpty

public boolean isTableEmpty(java.lang.String tableName)
                     throws java.lang.Exception
Checks that a given table is empty.

Parameters:
tableName - the name of the table to look for.
Returns:
true if the table is empty.
Throws:
java.lang.Exception - the exception
java.lang.Exception - if an error occurs.

isConnectionMenagedByExternalPool

public boolean isConnectionMenagedByExternalPool()
Checks if is connection menaged by external pool.

Returns:
true, if is connection menaged by external pool

setConnectionMenagedByExternalPool

public void setConnectionMenagedByExternalPool(boolean connectionMenagedByExternalPool)
Sets the connection menaged by external pool.

Parameters:
connectionMenagedByExternalPool - the new connection menaged by external pool

setConnection

public void setConnection(java.sql.Connection connection)
Sets the connection.

Parameters:
connection - the new connection