GenericDatabase Class

com.bea.p13n.util.jdbc
GenericDatabase Class

public class GenericDatabase

    extends Object
    implements Database

Concrete implementation of the Database interface. This implementation is designed to be used with database drivers that support Statement.setCharacterStream() and Statement.setBinaryStream(). Drivers also need to support Clob.getCharacterStream() to write/read CLOBs and Blob.getBinaryStream() to write/read BLOBs.

The setClob, getClob, setBlob, and getBlob methods in this class should be overidden if a driver does not support the above methods when working with BLOBs and CLOBs.

Related Topics

Database


Hierarchy
Object
  GenericDatabase
All Implemented Interfaces

Database
Direct Known Subclasses

OracleDatabase, SetStringDatabase

Constructor Summary

GenericDatabase(Connection connection)

 

Method Summary

public boolean
accept(Connection connection)
Check to see if the connection URL matches the format defined in "accept.driver.url.regex".
public void
close()
Close the Connection associated with this Database.
public void
close(Statement stmt)
Closes a Statement.
public void
close(PreparedStatement ps)
Closes a PreparedStatement.
public void
close(ResultSet rs)
Close a result set, ignoring any errors.
public void
close(ResultSet rs, Statement stmt)
Close a statement and resultset, ignoring errors.
public void
close(Reader reader)
Closes a Reader.
public void
close(InputStream is)
Closes an InputStream.
public void
close(Writer writer)
Closes a Writer.
public void
close(OutputStream os)
Closes an OutputStream.
public boolean
commitConnection(Connection connection)
protected PreparedStatement
createPreparedStatement(String key)
Creates a PreparedStatement based on queries found in ResourceBundle.
public void
endBlob()
Commits Blob transaction.
public void
endClob()
Commits Clob transaction.
public String
getApplicationName()
Get the name of the current WebLogic J2EE application or return null if not running within a WebLogic J2EE context.
public byte[]
getBlob(ResultSet rs, int index)
Retrieve Blob value as a byte[] from a ResultSet.
public OutputStream
getBlobLocator(Connection con, String tableName, String blobColumnName, String whereClause)
Gets an OutputStream to the BlobLocator object.
public String
getClob(ResultSet rs, int index)
Get the value of a CLOB column as a string.
public Clob
getClobLocator(Connection con, String tableName, String clobColumnName, String whereClause)
public String
getClobTableName()
Used to retrieve the name of the table that stores Clobs.
public Connection
getConnection()
Return Connection object of the current Database instance.
public String
getEmptyBlobInitializer()
Returns an empty String since Pointbase doesn't use initializers.
public String
getEmptyClobInitializer()
public String
getName()
The name of this GenericDatabase instance.
protected String
getProperty(String propertyName)
Used to retrieve a property in the ResourceBundle.
protected void
printSqlWarning(SQLWarning warning)
Prints the SQLWarning to System.err.
public String
readFromClob(Clob clob)
Reads the contents of a Clob and returns as a String.
public String
readFromClob(ResultSet resultSet, int index)
Reads the contents of a Clob and returns as a String.
public void
setClob(PreparedStatement stmt, int index, String str)
Set the value of a CLOB column with a string.
public boolean
setClob(PreparedStatement stmt, int index, Clob clob, String str)
If there is a delegate, this method uses JdbcHelperDelegate.writeClobData() If this is done, then the PreparedStatement does not need to be used and the return value, executeRequired, is false.
public void
startBlob()
Starts the transaction to commit Blob.
public void
startClob()
Starts the transaction to commit Clob.
public void
writeToClob(Clob clob, String data)
Writes the data from a String into a Clob.
public Reader
writeToClob(PreparedStatement statement, int index, String data)
Writes the data from a String into a Clob using a PreparedStatement.
 
Methods from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
   
Methods from interface com.bea.p13n.util.jdbc.Database
accept, close, close, close, close, close, close, close, close, close, commitConnection, endBlob, endClob, getApplicationName, getBlob, getBlobLocator, getClob, getClobLocator, getConnection, getEmptyBlobInitializer, getEmptyClobInitializer, getName, readFromClob, setClob, setClob, startBlob, startClob, writeToClob
 

Constructor Detail

GenericDatabase

public GenericDatabase(Connection connection)
 

Method Detail

accept(Connection) Method

public boolean accept(Connection connection)
Check to see if the connection URL matches the format defined in "accept.driver.url.regex".

Parameters

connection
The Connection object that contains the URL.

Returns

If this Database can accept a connection URL of the supplied form return true, otherwise return false.

close() Method

public void close()
Close the Connection associated with this Database.


close(Statement) Method

public void close(Statement stmt)
Closes a Statement.

Parameters

stmt
The Statement object to be closed.

close(PreparedStatement) Method

public void close(PreparedStatement ps)
Closes a PreparedStatement.

Parameters

ps
The PreparedStatement object to be closed.

close(ResultSet) Method

public void close(ResultSet rs)
Close a result set, ignoring any errors.

Parameters

rs
The ResultSet object to be closed.

close(ResultSet, Statement) Method

public void close(ResultSet rs, 
                  Statement stmt)
Close a statement and resultset, ignoring errors.

Parameters

rs
The ResultSet object to be closed.
stmt
The Statement object to be closed.

close(Reader) Method

public void close(Reader reader)
Closes a Reader.

Parameters

reader
The Reader stream.

close(InputStream) Method

public void close(InputStream is)
Closes an InputStream.

Parameters

is
The InputStream.

close(Writer) Method

public void close(Writer writer)
Closes a Writer.

Parameters

writer
The Writer stream.

close(OutputStream) Method

public void close(OutputStream os)
Closes an OutputStream.

Parameters

os
The OutputStream to close.

commitConnection(Connection) Method

public boolean commitConnection(Connection connection)
throws SQLException

Exceptions

SQLException

createPreparedStatement(String) Method

protected PreparedStatement createPreparedStatement(String key)
throws SQLException
Creates a PreparedStatement based on queries found in ResourceBundle.

Parameters

key
The value in ResourceBundle which contains SQL Statement.

Returns

A PreparedStatement using the current Connection.

Exceptions

SQLException

endBlob() Method

public void endBlob()
throws SQLException
Commits Blob transaction.

Exceptions

SQLException
Thrown when committing Blob.

endClob() Method

public void endClob()
throws SQLException
Commits Clob transaction.

Exceptions

SQLException
Thrown when committing Clob.

getApplicationName() Method

public String getApplicationName()
Get the name of the current WebLogic J2EE application or return null if not running within a WebLogic J2EE context.

Returns

The WebLogic J2EE application.

getBlob(ResultSet, int) Method

public byte[] getBlob(ResultSet rs, 
                    int index)
throws SQLException
Retrieve Blob value as a byte[] from a ResultSet.

Parameters

rs
The ResultSet containing the Blob.
index
The position of the Blob in the ResultSet.

Returns

byte[] containing the binary value of the Blob.

Exceptions

SQLException
on an error

getBlobLocator(Connection, String, String, String) Method

public OutputStream getBlobLocator(Connection con, 
                                   String tableName, 
                                   String blobColumnName, 
                                   String whereClause)
throws SQLException
Gets an OutputStream to the BlobLocator object. Should be null for Pointbase.

Returns

OutputStream An outputStream pointing to the BlobLocator.

Exceptions

SQLException
on an error.

getClob(ResultSet, int) Method

public String getClob(ResultSet rs, 
                      int index)
throws SQLException
Description copied from Database.getClob(ResultSet, int)
Get the value of a CLOB column as a string.

This method will only return the value of the CLOB in the current row of the ResultSet. This method does not increment the ResultSet cursor. You must use ResultSet.next() to increment the cursor before calling this method. This allows the method to be used to process multi-row ResultSets.

If no JdbcHelperDelegate is specified in the console (configured via JdbcHelperMBean) then this method will return a string obtained using the default method that was used before a delegate model was implemented for this class. That method uses Clob.getCharacterStream() to construct a BufferedReader that is read in with the readLine() method.

Parameters

rs
the result set.
index
the column index in the result set.

Returns

the column as a string.

Exceptions

SQLException

getClobLocator(Connection, String, String, String) Method

public Clob getClobLocator(Connection con, 
                           String tableName, 
                           String clobColumnName, 
                           String whereClause)
throws SQLException

Exceptions

SQLException

getClobTableName() Method

public String getClobTableName()
Used to retrieve the name of the table that stores Clobs.

Returns

The name of the database table used to persist CLOB datatypes.

getConnection() Method

public Connection getConnection()
Return Connection object of the current Database instance.

Returns

The Connection object of the current Database instance.

getEmptyBlobInitializer() Method

public String getEmptyBlobInitializer()
throws SQLException
Returns an empty String since Pointbase doesn't use initializers.

Returns

String Name of function to initialize a Blob.

Exceptions

SQLException
on an error.

getEmptyClobInitializer() Method

public String getEmptyClobInitializer()
throws SQLException

Exceptions

SQLException

getName() Method

public String getName()
The name of this GenericDatabase instance.

Returns

String The name of the GenericDatabase instance.

getProperty(String) Method

protected String getProperty(String propertyName)
Used to retrieve a property in the ResourceBundle.

Parameters

propertyName
The name of the property in the ResourceBundle.

Returns

The value associated with the property.

printSqlWarning(SQLWarning) Method

protected void printSqlWarning(SQLWarning warning)
Prints the SQLWarning to System.err.

Parameters

warning
The SQLWarning from a Connection, Statement, or ResultSet.

readFromClob(Clob) Method

public String readFromClob(Clob clob)
throws SQLException
Reads the contents of a Clob and returns as a String.

Parameters

clob
The ResultSet object to be closed.

Returns

The contents of the Clob as a String.

Exceptions

SQLException
Thrown when error occurs while reading Clob.

readFromClob(ResultSet, int) Method

public String readFromClob(ResultSet resultSet, 
                           int index)
throws SQLException
Reads the contents of a Clob and returns as a String.

Parameters

resultSet
The ResultSet returned from a Statement query.
index
The column index where the Clob can be found in the ResultSet.

Returns

The contents of the Clob as a String.

Exceptions

SQLException
Thrown when error occurs when reading ResultSet.

setClob(PreparedStatement, int, String) Method

public void setClob(PreparedStatement stmt, 
                    int index, 
                    String str)
throws SQLException
Description copied from Database.setClob(PreparedStatement, int, String)
Set the value of a CLOB column with a string.

Parameters

stmt
the prepared statement.
index
the column index.
str
the CLOB string.

Exceptions

SQLException

setClob(PreparedStatement, int, Clob, String) Method

public boolean setClob(PreparedStatement stmt, 
                       int index, 
                       Clob clob, 
                       String str)
throws SQLException
Description copied from Database.setClob(PreparedStatement, int, Clob, String)
If there is a delegate, this method uses JdbcHelperDelegate.writeClobData() If this is done, then the PreparedStatement does not need to be used and the return value, executeRequired, is false. The CLOB should be first located with getClobLocator() If there is no delegate, then this method ignores the Clob argument and sets the value of the String into the CLOB placeholder in the PreparedStatement with setClob(PreparedStatement stmt, int index, String str) and returns executeRequired = true.

WARNING: if using a delegate to stream data to a CLOB, then you should have used Connection.setAutoCommit(false) before calling the CLOB locator method that provided the Clob you are using in this method call. This will make the CLOB available across multiple SQL statements.

Parameters

stmt
the prepared statement.
index
the column index.
clob
the CLOB locator (ignored if no delegate).
str
the String to be set in the CLOB (if delegate) or in the PreparedStatement (if no delegate)

Returns

boolean executeRequired. If false then a delegate was used to directly set the CLOB in the database without using the PreparedStatement. If true then no delegate was used, or the delegate set the Clob value on the statement and the PreparedStatement must be executed to update the database.

Exceptions

SQLException

startBlob() Method

public void startBlob()
throws SQLException
Starts the transaction to commit Blob.

Exceptions

SQLException
Thrown when setting auto-commit to false.

startClob() Method

public void startClob()
throws SQLException
Starts the transaction to commit Clob.

Exceptions

SQLException
Thrown when setting auto-commit to false.

writeToClob(Clob, String) Method

public void writeToClob(Clob clob, 
                        String data)
throws SQLException
Writes the data from a String into a Clob.

Parameters

clob
The target Clob object.
data
The data to be converted to a Clob in the form of a String.

Exceptions

SQLException
Thrown because this is an unsupported operation.

writeToClob(PreparedStatement, int, String) Method

public Reader writeToClob(PreparedStatement statement, 
                          int index, 
                          String data)
throws SQLException
Writes the data from a String into a Clob using a PreparedStatement.

Parameters

statement
The PreparedStatement that holds the query.
index
The index in the PreparedStatement that holds the Clob.
data
The data to be converted to be written as a Clob.

Exceptions

SQLException
Thrown when retrieving character stream from statement.