at.tuwien.ifs.somtoolbox.database
Class DBConnector

java.lang.Object
  extended by at.tuwien.ifs.somtoolbox.database.DBConnector
Direct Known Subclasses:
MySQLConnector

public abstract class DBConnector
extends java.lang.Object

An abstract Database connector.

Version:
$Id: DBConnector.java 3590 2010-05-21 10:43:45Z mayer $
Author:
Rudolf Mayer

Field Summary
protected  java.sql.Connection con
           
protected  java.lang.String databaseName
           
protected  java.lang.String databaseUrl
           
protected  java.lang.String documentTableName
           
protected  java.lang.String documentTermsTableName
           
static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsDocument
           
static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsDocumentTerms
           
static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsTerm
           
protected  java.lang.String password
           
(package private)  java.sql.Statement statement
           
protected  java.lang.String statementEndCharacter
           
protected  java.lang.String tableNamePrefix
           
protected  java.lang.String termTableName
           
protected  java.lang.String user
           
 
Constructor Summary
DBConnector(java.lang.String databaseUrl, java.lang.String databaseName, java.lang.String user, java.lang.String password, java.lang.String tableNamePrefix)
           
 
Method Summary
protected  void checkDBConnection()
          Checks whether the connection is open, and opens a new connection if needed.
 void closeConnection()
          Closes the DB connection.
 boolean connect()
           
protected  void createTable(java.lang.String tableName, java.util.Hashtable<java.lang.String,java.lang.Class<?>> fields, java.lang.String primaryKey, java.lang.String uniqueKey)
          Creates a database table.
 int doInsert(java.lang.String tableName, java.lang.String[] columns, java.lang.Object[] values)
          Inserts a new row into a table.
 int doInsert(java.lang.String tableName, java.lang.String[] columns, java.lang.Object[][] values)
          Inserts multiple rows into the table.
 int doInsert(java.lang.String tableName, java.lang.String column, java.lang.Object value)
           
 int doInsert(java.lang.String tableName, java.lang.String column, java.lang.String value)
           
 java.sql.ResultSet executeSelect(java.lang.String query)
          Executes a SELECT Statement and returns a ResultSet
 int executeUpdate(java.lang.String query)
          Executes an update statement.
 java.lang.String getDocumentTableName()
           
 java.lang.String getDocumentTermsTableName()
           
private  java.lang.StringBuffer getInsertQuery(java.lang.String tableName, java.lang.String[] columns, java.lang.Object[] values)
           
private  java.lang.StringBuffer getInsertQuery(java.lang.String tableName, java.lang.String column, java.lang.Object value)
           
private  java.lang.StringBuffer getInsertQuery(java.lang.String tableName, java.lang.String column, java.lang.String value)
           
 java.sql.PreparedStatement getPreparedStatement(java.lang.String sql)
          Creates a prepared statement for the given query
 java.lang.String getTermTableName()
           
abstract  java.sql.Connection openConnection()
           
 void setupTables()
          Prepares the database by creating the needed tables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fieldsDocument

public static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsDocument

fieldsDocumentTerms

public static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsDocumentTerms

fieldsTerm

public static java.util.Hashtable<java.lang.String,java.lang.Class<?>> fieldsTerm

statement

java.sql.Statement statement

con

protected java.sql.Connection con

databaseName

protected java.lang.String databaseName

databaseUrl

protected java.lang.String databaseUrl

password

protected java.lang.String password

tableNamePrefix

protected java.lang.String tableNamePrefix

user

protected java.lang.String user

documentTableName

protected java.lang.String documentTableName

documentTermsTableName

protected java.lang.String documentTermsTableName

termTableName

protected java.lang.String termTableName

statementEndCharacter

protected java.lang.String statementEndCharacter
Constructor Detail

DBConnector

public DBConnector(java.lang.String databaseUrl,
                   java.lang.String databaseName,
                   java.lang.String user,
                   java.lang.String password,
                   java.lang.String tableNamePrefix)
Method Detail

checkDBConnection

protected void checkDBConnection()
                          throws java.sql.SQLException
Checks whether the connection is open, and opens a new connection if needed.

Throws:
java.sql.SQLException - if connection to DB fails

connect

public boolean connect()
                throws java.sql.SQLException
Throws:
java.sql.SQLException

closeConnection

public void closeConnection()
                     throws java.sql.SQLException
Closes the DB connection.

Throws:
java.sql.SQLException - if the connection is closed or in auto-commit mode.

openConnection

public abstract java.sql.Connection openConnection()
                                            throws java.sql.SQLException
Throws:
java.sql.SQLException

doInsert

public int doInsert(java.lang.String tableName,
                    java.lang.String[] columns,
                    java.lang.Object[] values)
             throws java.sql.SQLException
Inserts a new row into a table.

Parameters:
tableName - The table to insert.
columns - The names of the columns.
values - The values, in the same order as the column names
Returns:
the row count.
Throws:
java.sql.SQLException - if the insert fails

doInsert

public int doInsert(java.lang.String tableName,
                    java.lang.String column,
                    java.lang.Object value)
             throws java.sql.SQLException
Throws:
java.sql.SQLException

doInsert

public int doInsert(java.lang.String tableName,
                    java.lang.String column,
                    java.lang.String value)
             throws java.sql.SQLException
Throws:
java.sql.SQLException

doInsert

public int doInsert(java.lang.String tableName,
                    java.lang.String[] columns,
                    java.lang.Object[][] values)
             throws java.sql.SQLException
Inserts multiple rows into the table.

Throws:
java.sql.SQLException
See Also:
doInsert(String, String[], Object[])

getPreparedStatement

public java.sql.PreparedStatement getPreparedStatement(java.lang.String sql)
                                                throws java.sql.SQLException
Creates a prepared statement for the given query

Throws:
java.sql.SQLException

getInsertQuery

private java.lang.StringBuffer getInsertQuery(java.lang.String tableName,
                                              java.lang.String[] columns,
                                              java.lang.Object[] values)

getInsertQuery

private java.lang.StringBuffer getInsertQuery(java.lang.String tableName,
                                              java.lang.String column,
                                              java.lang.Object value)

getInsertQuery

private java.lang.StringBuffer getInsertQuery(java.lang.String tableName,
                                              java.lang.String column,
                                              java.lang.String value)

executeSelect

public java.sql.ResultSet executeSelect(java.lang.String query)
                                 throws java.sql.SQLException
Executes a SELECT Statement and returns a ResultSet

Parameters:
query - String
Returns:
ResultSet
Throws:
java.sql.SQLException - if a db-error occures.

executeUpdate

public int executeUpdate(java.lang.String query)
                  throws java.sql.SQLException
Executes an update statement.

Parameters:
query - SQL Statement
Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing.
Throws:
java.sql.SQLException - if a db-error occures.

getDocumentTableName

public java.lang.String getDocumentTableName()
Returns:
the name of the table containing the document labels.

getDocumentTermsTableName

public java.lang.String getDocumentTermsTableName()
Returns:
the name of the table containing the vector elements.

getTermTableName

public java.lang.String getTermTableName()
Returns:
the name of the table containing the template vector elements.

createTable

protected void createTable(java.lang.String tableName,
                           java.util.Hashtable<java.lang.String,java.lang.Class<?>> fields,
                           java.lang.String primaryKey,
                           java.lang.String uniqueKey)
                    throws java.sql.SQLException
Creates a database table.

Parameters:
tableName - name of the table
fields - the columns of the table
primaryKey - primary key
uniqueKey - additional unique key
Throws:
java.sql.SQLException - if a db-error occures.

setupTables

public void setupTables()
                 throws java.sql.SQLException
Prepares the database by creating the needed tables.

Throws:
java.sql.SQLException - if a db-error occures.