de.unidu.is.util
Class DB

java.lang.Object
  extended byde.unidu.is.util.DB
Direct Known Subclasses:
HSQLDBEmbeddedDB, HSQLDBFileDB, MySQLDB

public class DB
extends java.lang.Object

This class encapsulates a RDBMS (using JDBC), where connections are managed by a connection pool.

This class uses the logger "unidu.db".

Since:
2003-06-01
Version:
$Revision: 1.29 $, $Date: 2005/03/18 22:02:04 $
Author:
Henrik Nottelmann

Field Summary
protected  javax.sql.DataSource dataSource
          The data source of this specific database.
protected static java.util.Map dataSources
          A map of data sources, one for each database URL.
protected  java.lang.String dbURI
          The database URI.
protected  java.lang.String dbUser
          The database user.
protected  SQLFormatter formatter
          SQL formatter.
protected  org.apache.log4j.Logger logger
          Logger.
protected  java.util.Map result2conn
          Mapping from result sets to their connections, used for freeing them.
protected  java.util.Map result2stmt
          Mapping from result sets to their statements, used for closing them.
 
Constructor Summary
DB(PropertyMap map, java.lang.String prefix)
          Creates a new instance, where the parameters are taken from a map.
DB(PropertyMap map, java.lang.String prefix, org.apache.log4j.Logger logger)
          Creates a new instance, where the parameters are taken from a map.
DB(PropertyMap map, java.lang.String prefix, java.lang.String loggerName)
          Creates a new instance, where the parameters are taken from a map.
DB(java.lang.String uri, java.lang.String driver, java.lang.String user, java.lang.String password)
          Creates a new instance.
DB(java.lang.String uri, java.lang.String driver, java.lang.String user, java.lang.String password, org.apache.log4j.Logger logger)
          Creates a new instance.
DB(java.lang.String uri, java.lang.String driver, java.lang.String user, java.lang.String password, java.lang.String logfile)
          Deprecated.  
DB(java.lang.String uri, java.lang.String driver, java.lang.String user, java.lang.String password, java.lang.String logfile, org.apache.log4j.Logger logger)
          Deprecated.  
DB(java.lang.String uri, java.lang.String driver, java.lang.String user, java.lang.String password, java.lang.String logfile, java.lang.String loggerName)
          Deprecated.  
 
Method Summary
 void close()
          Closes all open connections.
 void close(java.sql.ResultSet result)
          Closes the connection belonging to a specific java.sql.ResultSet, derived from a executeQuery(String) call.
 void createTable(java.lang.String tableName, java.lang.String definition)
          Creates a table (and removes an already existing table first).
 void createTableIfNotExists(java.lang.String tableName, java.lang.String definition)
          Creates a table if it does not exist.
 void dropTable(java.lang.String tableName)
          Removes an already existing table.
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Executes a SQL query and returns a java.sql.ResultSet.
 java.sql.ResultSet executeQuery(java.lang.String sql, boolean info)
          Executes a SQL query and returns a java.sql.ResultSet.
 int executeUpdate(java.sql.PreparedStatement sql, boolean info)
          Executes a SQLsStatment and returns the number of changed rows.
 int executeUpdate(java.lang.String sql)
          Executes a SQL update and returns the number of changed rows.
 int executeUpdate(java.lang.String sql, boolean info)
          Executes a SQL update and returns the number of changed rows.
 int executeUpdateIgnoreError(java.lang.String sql)
          Executes a SQL update and returns the number of changed rows.
 boolean existsTable(java.lang.String tableName)
          Checks whether the table name exists.
protected  void freeConnection(java.sql.Connection conn)
          Frees a connection i.e. puts it back to the connection pool.
 java.sql.ResultSet getColumns(java.lang.String table)
          Fetches the attributes of a table.
 java.sql.Connection getConnection()
          Returns a connection for this instance out of the connection pool.
 java.lang.String getDbURI()
          Returns the URI of the database.
 java.lang.String getDbUser()
          Returns the user of the database.
 int insert(java.lang.String table, java.util.Map elements)
          Inserts field/value pairs into the given database table.
 int insert(java.lang.String table, java.util.Map elements, boolean info)
          Inserts field/value pairs into the given database table.
 int insertIfMissing(java.lang.String table, java.util.Map elements, java.lang.String fieldToCheck, boolean info)
          Inserts elements into table if a certain field values doesn't exist.
 int insertIfMissing(java.lang.String table, java.util.Map elements, java.util.Vector fieldsToCheck, boolean info)
          Inserts elements into table if certain field values don't exist.
 java.lang.String makeIdentifier(java.lang.String str)
          Converts a specified string into an identifier useful for databases.
 int remove(java.lang.String table, java.util.Map removeCondition, boolean info)
          Removes all objects in a table identified by a given remove condition.
 java.lang.String toString()
          Returns a text representation of the current object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected org.apache.log4j.Logger logger
Logger.


result2conn

protected java.util.Map result2conn
Mapping from result sets to their connections, used for freeing them.


result2stmt

protected java.util.Map result2stmt
Mapping from result sets to their statements, used for closing them.


dataSources

protected static java.util.Map dataSources
A map of data sources, one for each database URL.


dbURI

protected java.lang.String dbURI
The database URI.


dbUser

protected java.lang.String dbUser
The database user.


dataSource

protected javax.sql.DataSource dataSource
The data source of this specific database.


formatter

protected SQLFormatter formatter
SQL formatter.

Constructor Detail

DB

public DB(java.lang.String uri,
          java.lang.String driver,
          java.lang.String user,
          java.lang.String password)
Creates a new instance.

Parameters:
uri - RDBMS URI
driver - class name of JDBC driver
user - user name
password - user password

DB

public DB(java.lang.String uri,
          java.lang.String driver,
          java.lang.String user,
          java.lang.String password,
          org.apache.log4j.Logger logger)
Creates a new instance.

Parameters:
uri - RDBMS URI
driver - class name of JDBC driver
user - user name
password - user password
logger - DB logger

DB

public DB(PropertyMap map,
          java.lang.String prefix)
Creates a new instance, where the parameters are taken from a map. The prefix of the parameters can be chosen, the suffix are ".uri", ".driver", ".user", ".password" and ".log".

Parameters:
map - map containing the parameters
prefix - parameter prefix

DB

public DB(PropertyMap map,
          java.lang.String prefix,
          java.lang.String loggerName)
Creates a new instance, where the parameters are taken from a map. The prefix of the parameters can be chosen, the suffix are ".uri", ".driver", ".user", ".password" and ".log".

Parameters:
map - map containing the parameters
prefix - parameter prefix
loggerName - name of DB logger

DB

public DB(PropertyMap map,
          java.lang.String prefix,
          org.apache.log4j.Logger logger)
Creates a new instance, where the parameters are taken from a map. The prefix of the parameters can be chosen, the suffix are ".uri", ".driver", ".user", ".password" and ".log".

Parameters:
map - map containing the parameters
prefix - parameter prefix
logger - DB logger

DB

public DB(java.lang.String uri,
          java.lang.String driver,
          java.lang.String user,
          java.lang.String password,
          java.lang.String logfile,
          org.apache.log4j.Logger logger)
Deprecated.  

Creates a new instance.

Parameters:
uri - RDBMS URI
driver - class name of JDBC driver
user - user name
password - user password
logfile - ignored parameter
logger - DB logger

DB

public DB(java.lang.String uri,
          java.lang.String driver,
          java.lang.String user,
          java.lang.String password,
          java.lang.String logfile)
Deprecated.  

Creates a new instance.

Parameters:
uri - RDBMS URI
driver - class name of JDBC driver
user - user name
password - user password
logfile - ignored parameter

DB

public DB(java.lang.String uri,
          java.lang.String driver,
          java.lang.String user,
          java.lang.String password,
          java.lang.String logfile,
          java.lang.String loggerName)
Deprecated.  

Creates a new instance.

Parameters:
uri - RDBMS URI
driver - class name of JDBC driver
user - user name
password - user password
logfile - ignored parameter
loggerName - name of DB logger
Method Detail

getConnection

public java.sql.Connection getConnection()
Returns a connection for this instance out of the connection pool.

Returns:
connection

freeConnection

protected void freeConnection(java.sql.Connection conn)
Frees a connection i.e. puts it back to the connection pool.

Parameters:
conn - connection

makeIdentifier

public java.lang.String makeIdentifier(java.lang.String str)
Converts a specified string into an identifier useful for databases.

The identifier is the specified string, where dots are replaced by underscores, and only letters (lowercase), digits and underscores are used (all other characters are removed).

Parameters:
str - string to convert
Returns:
converted string (identifier)

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException
Executes a SQL query and returns a java.sql.ResultSet.

The corresponding statement is stored s.th. it can be closed lateron.

Parameters:
sql - SQL query
Returns:
result set
Throws:
java.sql.SQLException

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql,
                                       boolean info)
                                throws java.sql.SQLException
Executes a SQL query and returns a java.sql.ResultSet.

The corresponding statement is stored s.th. it can be closed lateron.

Parameters:
sql - SQL query
info - flag indicating whether logging as info is wanted
Returns:
result set
Throws:
java.sql.SQLException

insertIfMissing

public int insertIfMissing(java.lang.String table,
                           java.util.Map elements,
                           java.lang.String fieldToCheck,
                           boolean info)
Inserts elements into table if a certain field values doesn't exist. This means: The method checks if the fieldsToCheck and its value contained in the map elements exists in the table, If it already exists, nothing is inserted. If it doesn't exist, the elements given in the map are inserted into the table. An example application of this method would be to check if a certain primary key element was inserted before into the table.

Parameters:
table - the table name
elements - the elements to be inserted
fieldToCheck - the field to be checked first. The corresponding value is taken from the elements map
info - flag indicating whether logging as info is wanted
Returns:
the number of changed rows, or -1 if an error occurs

insertIfMissing

public int insertIfMissing(java.lang.String table,
                           java.util.Map elements,
                           java.util.Vector fieldsToCheck,
                           boolean info)
Inserts elements into table if certain field values don't exist. This means: the vector fieldsToCheck contains all fields which have to be checked first. The method checks if these fields exist in the table with their values given in the map elements. If they already exist with the given value, nothing is inserted. If they don't exist, the elements given in the map are inserted into the table.

Parameters:
table - the table name
elements - the elements to be inserted
fieldsToCheck - the fields to be checked first. The corresponding values are take from the elements map
info - flag indicating whether logging as info is wanted
Returns:
the number of changed rows, or -1 if an error occurs

insert

public int insert(java.lang.String table,
                  java.util.Map elements)
Inserts field/value pairs into the given database table.

Parameters:
table - the table name
elements - the elements - the key of the map is the field name, the value its value.
Returns:
number of changed rows, or -1, if an error occurs

insert

public int insert(java.lang.String table,
                  java.util.Map elements,
                  boolean info)
Inserts field/value pairs into the given database table.

Parameters:
table - the table name
elements - the elements - the key of the map is the field name, the value its value.
info - flag indicating whether logging as info is wanted
Returns:
number of changed rows, or -1, if an error occurs

remove

public int remove(java.lang.String table,
                  java.util.Map removeCondition,
                  boolean info)
Removes all objects in a table identified by a given remove condition. The query condition is given by a map of fields (key) and corresponding values. The conjunction of all single remove conditions is checked. If the map is null or empty, every entry in the table will be removed!

Parameters:
table - the name of the table
removeCondition - the remove condition
info - flag indicating whether logging as info is wanted
Returns:
number of changed rows, or -1 if an error occurred

executeUpdate

public int executeUpdate(java.lang.String sql)
Executes a SQL update and returns the number of changed rows.

The corresponding statement is already closed.

Parameters:
sql - SQL upodate
Returns:
number of changed rows, or -1, if an error occurs

executeUpdateIgnoreError

public int executeUpdateIgnoreError(java.lang.String sql)
Executes a SQL update and returns the number of changed rows. Errors are ignored.

The corresponding statement is already closed.

Parameters:
sql - SQL upodate
Returns:
number of changed rows, or -1, if an error occurs

executeUpdate

public int executeUpdate(java.lang.String sql,
                         boolean info)
Executes a SQL update and returns the number of changed rows.

The corresponding statement is already closed.

Parameters:
sql - SQL upodate
info - flag indicating whether logging as info is wanted
Returns:
number of changed rows, or -1, if an error occurs

executeUpdate

public int executeUpdate(java.sql.PreparedStatement sql,
                         boolean info)
Executes a SQLsStatment and returns the number of changed rows.

The corresponding statement is already closed.

Parameters:
sql - SQL upodate
info - flag indicating whether logging as info is wanted
Returns:
number of changed rows, or -1, if an error occurs

getColumns

public java.sql.ResultSet getColumns(java.lang.String table)
                              throws java.sql.SQLException
Fetches the attributes of a table.

Parameters:
table - Tablename
Returns:
number of changed rows, or -1, if an error occurs
Throws:
java.sql.SQLException

createTable

public void createTable(java.lang.String tableName,
                        java.lang.String definition)
Creates a table (and removes an already existing table first).

The definition must not contain the surrounding brackets!

This command is logged as info.

Parameters:
tableName - name of the table to be created
definition - definition of the table

existsTable

public boolean existsTable(java.lang.String tableName)
Checks whether the table name exists.

Parameters:
tableName - name of the table to be checked

createTableIfNotExists

public void createTableIfNotExists(java.lang.String tableName,
                                   java.lang.String definition)
Creates a table if it does not exist.

The definition must not contain the surrounding brackets!

This command is logged as info.

Parameters:
tableName - name of the table to be created
definition - definition of the table

dropTable

public void dropTable(java.lang.String tableName)
Removes an already existing table.

Parameters:
tableName - name of the table to be removed

close

public void close(java.sql.ResultSet result)
Closes the connection belonging to a specific java.sql.ResultSet, derived from a executeQuery(String) call.

Parameters:
result - ResultSet

close

public void close()
Closes all open connections.


toString

public java.lang.String toString()
Returns a text representation of the current object.

Returns:
text representation of the current object

getDbURI

public java.lang.String getDbURI()
Returns the URI of the database.

Returns:
URI of the database

getDbUser

public java.lang.String getDbUser()
Returns the user of the database.

Returns:
user of the database