© 2005 BEA Systems, Inc.

com.bea.p13n.util.jdbc
Class GenericDatabase

java.lang.Object
  extended bycom.bea.p13n.util.jdbc.GenericDatabase
All Implemented Interfaces:
Database
Direct Known Subclasses:
OracleDatabase, SetStringDatabase

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.

See Also:
Database

Constructor Summary
GenericDatabase(Connection connection)
           
 
Method Summary
 boolean accept(Connection connection)
          Check to see if the connection URL matches the format defined in "accept.driver.url.regex".
 void close()
          Close the Connection associated with this Database.
 void close(InputStream is)
          Closes an InputStream.
 void close(OutputStream os)
          Closes an OutputStream.
 void close(PreparedStatement ps)
          Closes a PreparedStatement.
 void close(Reader reader)
          Closes a Reader.
 void close(ResultSet rs)
          Close a result set, ignoring any errors.
 void close(ResultSet rs, Statement stmt)
          Close a statement and resultset, ignoring errors.
 void close(Statement stmt)
          Closes a Statement.
 void close(Writer writer)
          Closes a Writer.
 boolean commitConnection(Connection connection)
           
protected  PreparedStatement createPreparedStatement(String key)
          Creates a PreparedStatement based on queries found in ResourceBundle.
 void endBlob()
          Commits Blob transaction.
 void endClob()
          Commits Clob transaction.
 String getApplicationName()
          Get the name of the current WebLogic J2EE application or return null if not running within a WebLogic J2EE context.
 byte[] getBlob(ResultSet rs, int index)
          Retrieve Blob value as a byte[] from a ResultSet.
 OutputStream getBlobLocator(Connection con, String tableName, String blobColumnName, String whereClause)
          Gets an OutputStream to the BlobLocator object.
 String getClob(ResultSet rs, int index)
          Get the value of a CLOB column as a string.
 Clob getClobLocator(Connection con, String tableName, String clobColumnName, String whereClause)
           
 String getClobTableName()
          Used to retrieve the name of the table that stores Clobs.
 Connection getConnection()
          Return Connection object of the current Database instance.
 String getEmptyBlobInitializer()
          Returns an empty String since Pointbase doesn't use initializers.
 String getEmptyClobInitializer()
           
 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.
 String readFromClob(Clob clob)
          Reads the contents of a Clob and returns as a String.
 String readFromClob(ResultSet resultSet, int index)
          Reads the contents of a Clob and returns as a String.
 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.
 void setClob(PreparedStatement stmt, int index, String str)
          Set the value of a CLOB column with a string.
 void startBlob()
          Starts the transaction to commit Blob.
 void startClob()
          Starts the transaction to commit Clob.
 void writeToClob(Clob clob, String data)
          Writes the data from a String into a Clob.
 Reader writeToClob(PreparedStatement statement, int index, String data)
          Writes the data from a String into a Clob using a PreparedStatement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericDatabase

public GenericDatabase(Connection connection)
Method Detail

accept

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

Specified by:
accept in interface Database
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

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

Specified by:
close in interface Database

close

public void close(InputStream is)
Closes an InputStream.

Specified by:
close in interface Database
Parameters:
is - The InputStream.
Throws:
IOException - Thrown when closing InputStream.

close

public void close(OutputStream os)
Closes an OutputStream.

Specified by:
close in interface Database
Parameters:
os - The OutputStream to close.

close

public void close(PreparedStatement ps)
Closes a PreparedStatement.

Specified by:
close in interface Database
Parameters:
ps - The PreparedStatement object to be closed.

close

public void close(Reader reader)
Closes a Reader.

Specified by:
close in interface Database
Parameters:
reader - The Reader stream.

close

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

Specified by:
close in interface Database
Parameters:
rs - The ResultSet object to be closed.

close

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

Specified by:
close in interface Database
Parameters:
rs - The ResultSet object to be closed.
stmt - The Statement object to be closed.

close

public void close(Statement stmt)
Closes a Statement.

Specified by:
close in interface Database
Parameters:
stmt - The Statement object to be closed.

close

public void close(Writer writer)
Closes a Writer.

Specified by:
close in interface Database
Parameters:
writer - The Writer stream.

commitConnection

public boolean commitConnection(Connection connection)
                         throws SQLException
Specified by:
commitConnection in interface Database
Throws:
SQLException

createPreparedStatement

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.
Throws:
SQLException

endBlob

public void endBlob()
             throws SQLException
Commits Blob transaction.

Specified by:
endBlob in interface Database
Throws:
SQLException - Thrown when committing Blob.

endClob

public void endClob()
             throws SQLException
Commits Clob transaction.

Specified by:
endClob in interface Database
Throws:
SQLException - Thrown when committing Clob.

getApplicationName

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

Specified by:
getApplicationName in interface Database
Returns:
The WebLogic J2EE application.

getBlob

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

Specified by:
getBlob in interface Database
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.
Throws:
SQLException - on an error

getBlobLocator

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.

Specified by:
getBlobLocator in interface Database
Returns:
OutputStream An outputStream pointing to the BlobLocator.
Throws:
SQLException - on an error.

getClob

public String getClob(ResultSet rs,
                      int index)
               throws SQLException
Description copied from interface: Database
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.

Specified by:
getClob in interface Database
Parameters:
rs - the result set.
index - the column index in the result set.
Returns:
the column as a string.
Throws:
SQLException - thrown on an error.

getClobLocator

public Clob getClobLocator(Connection con,
                           String tableName,
                           String clobColumnName,
                           String whereClause)
                    throws SQLException
Specified by:
getClobLocator in interface Database
Throws:
SQLException

getClobTableName

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

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

Specified by:
getConnection in interface Database
Returns:
The Connection object of the current Database instance.

getEmptyBlobInitializer

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

Specified by:
getEmptyBlobInitializer in interface Database
Returns:
String Name of function to initialize a Blob.
Throws:
SQLException - on an error.

getEmptyClobInitializer

public String getEmptyClobInitializer()
                               throws SQLException
Specified by:
getEmptyClobInitializer in interface Database
Throws:
SQLException

getName

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

Specified by:
getName in interface Database
Returns:
String The name of the GenericDatabase instance.

getProperty

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.
Throws:
IllegalStateException - thrown if the property cannot be retrieved.

printSqlWarning

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

Parameters:
warning - The SQLWarning from a Connection, Statement, or ResultSet.

readFromClob

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

Specified by:
readFromClob in interface Database
Parameters:
clob - The ResultSet object to be closed.
Returns:
The contents of the Clob as a String.
Throws:
SQLException - Thrown when error occurs while reading Clob.

readFromClob

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.
Throws:
SQLException - Thrown when error occurs when reading ResultSet.

setClob

public boolean setClob(PreparedStatement stmt,
                       int index,
                       Clob clob,
                       String str)
                throws SQLException
Description copied from interface: Database
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.

Specified by:
setClob in interface Database
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.
Throws:
SQLException - thrown on an error.

setClob

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

Specified by:
setClob in interface Database
Parameters:
stmt - the prepared statement.
index - the column index.
str - the CLOB string.
Throws:
SQLException - thrown on an error.

startBlob

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

Specified by:
startBlob in interface Database
Throws:
SQLException - Thrown when setting auto-commit to false.

startClob

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

Specified by:
startClob in interface Database
Throws:
SQLException - Thrown when setting auto-commit to false.

writeToClob

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

Specified by:
writeToClob in interface Database
Parameters:
clob - The target Clob object.
data - The data to be converted to a Clob in the form of a String.
Throws:
SQLException - Thrown because this is an unsupported operation.

writeToClob

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

Parameters:
index - The index in the PreparedStatement that holds the Clob.
data - The data to be converted to be written as a Clob.
Throws:
SQLException - Thrown when retrieving character stream from statement.

© 2005 BEA Systems, Inc.

Copyright © 2005 BEA Systems, Inc. All Rights Reserved