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 Object

An abstract Database connector.

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

Field Summary
protected  Connection con
           
protected  String databaseName
           
protected  String databaseUrl
           
protected  String documentTableName
           
protected  String documentTermsTableName
           
static Hashtable<String,Class<?>> fieldsDocument
           
static Hashtable<String,Class<?>> fieldsDocumentTerms
           
static Hashtable<String,Class<?>> fieldsTerm
           
protected  String password
           
(package private)  Statement statement
           
protected  String statementEndCharacter
           
protected  String tableNamePrefix
           
protected  String termTableName
           
protected  String user
           
 
Constructor Summary
DBConnector(String databaseUrl, String databaseName, String user, String password, 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(String tableName, Hashtable<String,Class<?>> fields, String primaryKey, String uniqueKey)
          Creates a database table.
 int doInsert(String tableName, String[] columns, Object[] values)
          Inserts a new row into a table.
 int doInsert(String tableName, String[] columns, Object[][] values)
          Inserts multiple rows into the table.
 int doInsert(String tableName, String column, Object value)
           
 int doInsert(String tableName, String column, String value)
           
 ResultSet executeSelect(String query)
          Executes a SELECT Statement and returns a ResultSet
 int executeUpdate(String query)
          Executes an update statement.
 String getDocumentTableName()
           
 String getDocumentTermsTableName()
           
private  StringBuffer getInsertQuery(String tableName, String[] columns, Object[] values)
           
private  StringBuffer getInsertQuery(String tableName, String column, Object value)
           
private  StringBuffer getInsertQuery(String tableName, String column, String value)
           
 PreparedStatement getPreparedStatement(String sql)
          Creates a prepared statement for the given query
 String getTermTableName()
           
abstract  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 Hashtable<String,Class<?>> fieldsDocument

fieldsDocumentTerms

public static Hashtable<String,Class<?>> fieldsDocumentTerms

fieldsTerm

public static Hashtable<String,Class<?>> fieldsTerm

statement

Statement statement

con

protected Connection con

databaseName

protected String databaseName

databaseUrl

protected String databaseUrl

password

protected String password

tableNamePrefix

protected String tableNamePrefix

user

protected String user

documentTableName

protected String documentTableName

documentTermsTableName

protected String documentTermsTableName

termTableName

protected String termTableName

statementEndCharacter

protected String statementEndCharacter
Constructor Detail

DBConnector

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

checkDBConnection

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

Throws:
SQLException - if connection to DB fails

connect

public boolean connect()
                throws SQLException
Throws:
SQLException

closeConnection

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

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

openConnection

public abstract Connection openConnection()
                                   throws SQLException
Throws:
SQLException

doInsert

public int doInsert(String tableName,
                    String[] columns,
                    Object[] values)
             throws 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:
SQLException - if the insert fails

doInsert

public int doInsert(String tableName,
                    String column,
                    Object value)
             throws SQLException
Throws:
SQLException

doInsert

public int doInsert(String tableName,
                    String column,
                    String value)
             throws SQLException
Throws:
SQLException

doInsert

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

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

getPreparedStatement

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

Throws:
SQLException

getInsertQuery

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

getInsertQuery

private StringBuffer getInsertQuery(String tableName,
                                    String column,
                                    Object value)

getInsertQuery

private StringBuffer getInsertQuery(String tableName,
                                    String column,
                                    String value)

executeSelect

public ResultSet executeSelect(String query)
                        throws SQLException
Executes a SELECT Statement and returns a ResultSet

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

executeUpdate

public int executeUpdate(String query)
                  throws 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:
SQLException - if a db-error occures.

getDocumentTableName

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

getDocumentTermsTableName

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

getTermTableName

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

createTable

protected void createTable(String tableName,
                           Hashtable<String,Class<?>> fields,
                           String primaryKey,
                           String uniqueKey)
                    throws 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:
SQLException - if a db-error occures.

setupTables

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

Throws:
SQLException - if a db-error occures.