oracle.otnsamples.eis.DBBroker (Java2HTML)
package oracle.otnsamples.eis;
import java.io.Reader;
import java.io.Writer;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;
import javax.sql.RowSet;
import oracle.jdbc.OracleResultSet;
import oracle.jdbc.rowset.OracleCachedRowSet;
import oracle.otnsamples.util.Utilities;
import oracle.sql.CLOB;
public class DBBroker {
private static Hashtable cache = new Hashtable( );
private Connector connector = null;
protected DBBroker( ) {
}
protected DBBroker( String dbKey ) throws Exception {
Properties prop = Utilities.loadParams( "DBDetails" );
String className = (String) prop.get( dbKey );
connector = (Connector) Class.forName( className ).newInstance( );
prop.clear( );
}
public static DBBroker getDBBroker( String dbKey )
throws Exception {
if ( !cache.contains( dbKey ) ) {
DBBroker broker = new DBBroker( dbKey );
cache.put( dbKey, broker ); }
return (DBBroker) cache.get( dbKey );
}
public static Connector getDBConnector( String dbKey )
throws Exception {
Properties prop = Utilities.loadParams( "DBDetails" );
String className = (String) prop.get( dbKey );
prop.clear( );
return (Connector) Class.forName( className ).newInstance( );
}
public RowSet execute( String query, ArrayList params )
throws DBException {
Connection con = null;
OracleCachedRowSet crset = new OracleCachedRowSet( );
try {
con = connector.getConnection( );
PreparedStatement pst = con.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
pst.setObject( i + 1, (String) params.get( i ) );
}
}
ResultSet rst = pst.executeQuery( );
crset.populate( rst );
rst.close( );
pst.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in execute "
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in execute() method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
return crset;
}
public ArrayList editNewsItem( String query, ArrayList params )
throws DBException {
Connection con = null; CLOB clob = null;
int bytesread = 0;
Reader reader;
StringBuffer clobData = new StringBuffer( );
ResultSet rs = null;
ResultSetMetaData rsmd;
int columnCount;
ArrayList data = new ArrayList( );
try {
con = connector.getConnection( );
PreparedStatement pst = con.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
pst.setObject( i + 1, params.get( i ) );
}
}
rs = pst.executeQuery( );
rsmd = rs.getMetaData( );
columnCount = rsmd.getColumnCount( );
while ( rs.next( ) ) {
String[] row = new String[ columnCount ];
for ( int i = 0; i < columnCount; i++ ) {
if ( ( i == 0 ) || ( i == 10 ) || ( i == 11 ) || ( i == 12 ) ) {
row[ i ] = String.valueOf( rs.getInt( i + 1 ) );
}
else {
if ( i == 5 ) {
OracleResultSet ors = (OracleResultSet) rs;
clob = ors.getCLOB( i + 1 );
if ( clob != null ) {
reader = clob.getCharacterStream( );
char[] charbuffer = new char[ clob.getBufferSize( ) ];
while ( ( bytesread = reader.read( charbuffer ) ) != -1 ) {
clobData.append( charbuffer, 0, bytesread );
}
reader.close( );
clob.freeTemporary( );
row[ i ] = clobData.toString( );
}
}
else {
row[ i ] = rs.getString( i + 1 );
}
}
}
data.add( row );
}
pst.close( );
return data;
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in getClobExecute "
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in getClobExecute() method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
}
public String executeForClob( String query, ArrayList params )
throws DBException {
Connection con = null;
ResultSet rs;
OracleResultSet ors;
CLOB clob = null;
int bytesread = 0;
Reader reader;
StringBuffer clobData = new StringBuffer( );
try {
con = connector.getConnection( );
PreparedStatement pst = con.prepareStatement( query );
String parameter;
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
parameter = (String) params.get( i );
pst.setObject( i + 1, getCLOB( parameter, con ) );
}
}
rs = pst.executeQuery( );
ors = (OracleResultSet) rs;
while ( ors.next( ) ) {
clob = ors.getCLOB( 1 );
}
if ( clob != null ) {
reader = clob.getCharacterStream( );
char[] charbuffer = new char[ clob.getBufferSize( ) ];
while ( ( bytesread = reader.read( charbuffer ) ) != -1 ) {
clobData.append( charbuffer, 0, bytesread );
}
reader.close( );
clob.freeTemporary( );
}
else {
clobData.append( "no data" );
}
rs.close( );
pst.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in executeForClob"
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in executeForClob method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
return clobData.toString( );
}
public int executeUpdate( String query, ArrayList params )
throws DBException {
Connection conn = null; int updateCount = 0;
try {
conn = connector.getConnection( );
PreparedStatement pst = conn.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
pst.setObject( i + 1, params.get( i ) );
}
}
updateCount = pst.executeUpdate( );
pst.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in "
+ "executeUpdate method of given "
+ "status : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in executeUpdate() method "
+ "of DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( conn );
}
catch ( DBConException ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
return updateCount;
}
public void executeCallable( ArrayList params )
throws DBException {
Connection conn = null; CallableStatement cs = null;
CLOB clob = null;
String flag = "";
try {
conn = connector.getConnection( );
if ( params != null ) {
if ( ( params.get( 0 ).equals( "description" ) ) ) {
cs =
(CallableStatement) conn.prepareCall( "begin updatexmlnewsdesc (?,?,?); end;" );
flag = "D";
}
else {
cs =
(CallableStatement) conn.prepareCall( "begin updatexmlnews (?,?,?); end;" );
flag = "O";
}
}
if ( flag.equals( "D" ) ) {
for ( int i = 0; i < params.size( ); i++ ) {
if ( i == 1 ) {
clob = this.getCLOB( (String) params.get( i ), conn );
cs.setObject( ( i + 1 ), clob );
}
else {
if ( i == 2 ) {
int j = Integer.parseInt( (String) params.get( i ) );
cs.setInt( i + 1, j );
}
else {
cs.setObject( i + 1, (String) params.get( i ) );
}
}
}
}
else if ( flag.equals( "O" ) ) {
for ( int i = 0; i < params.size( ); i++ ) {
cs.setObject( i + 1, (String) params.get( i ) );
}
}
cs.execute( );
cs.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in "
+ "executeCallable method of given "
+ "status : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in executeCallable method "
+ "of DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( conn );
}
catch ( DBConException ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
}
private CLOB getCLOB( String clobData, Connection conn )
throws DBException, SQLException {
CLOB tempClob = null;
try {
tempClob = CLOB.createTemporary( conn, true, CLOB.DURATION_SESSION );
tempClob.open( CLOB.MODE_READWRITE );
Writer tempClobWriter = tempClob.getCharacterOutputStream( );
tempClobWriter.write( clobData );
tempClobWriter.flush( );
tempClobWriter.close( );
tempClob.close( );
return tempClob;
}
catch ( SQLException sqlexp ) {
tempClob.freeTemporary( );
throw new DBException( "Exception thrown in getCLOB method "
+ "of DBBroker class of given status : "
+ sqlexp.getMessage( ) );
}
catch ( Exception exp ) {
tempClob.freeTemporary( );
throw new DBException( "Exception thrown in getCLOB method "
+ "of DBBroker class of given status : "
+ exp.getMessage( ) );
}
}
public String executeClob( String query, ArrayList params )
throws DBException {
ResultSet rs;
OracleResultSet ors;
CLOB clob = null;
int bytesread = 0;
Reader reader;
StringBuffer clobData = new StringBuffer( );
Connection con = null;
OracleCachedRowSet crset = new OracleCachedRowSet( );
try {
con = connector.getConnection( );
PreparedStatement pst = con.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
pst.setObject( i + 1, (String) params.get( i ) );
}
}
rs = pst.executeQuery( );
ors = (OracleResultSet) rs;
while ( ors.next( ) ) {
clob = ors.getCLOB( 1 );
}
if ( clob != null ) {
reader = clob.getCharacterStream( );
char[] charbuffer = new char[ clob.getBufferSize( ) ];
while ( ( bytesread = reader.read( charbuffer ) ) != -1 ) {
clobData.append( charbuffer, 0, bytesread );
}
reader.close( );
clob.freeTemporary( );
}
else {
clobData.append( "no Data" );
}
rs.close( );
pst.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in executeForClob"
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in executeForClob method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
return clobData.toString( );
}
public void insertNewsItem( String query, ArrayList params )
throws DBException {
int[] updatedRows;
CLOB clob = null;
PreparedStatement pstmt = null;
Connection con = null;
try {
con = connector.getConnection( );
pstmt = con.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
if ( i == 4 ) {
clob = this.getCLOB( (String) params.get( i ), con );
pstmt.setObject( ( i + 1 ), clob );
}
else {
pstmt.setObject( i + 1, params.get( i ) );
}
}
}
boolean flg = pstmt.execute( );
pstmt.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in clobExecute()"
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in clobExecute() method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
if ( pstmt != null ) {
pstmt.close( );
}
if ( clob != null ) {
freeCLOB( clob );
}
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
}
public void clobUpdate( String query, ArrayList params )
throws DBException {
int[] updatedRows;
CLOB clob = null;
PreparedStatement pstmt = null;
Connection con = null;
try {
con = connector.getConnection( );
pstmt = con.prepareStatement( query );
if ( params != null ) {
for ( int i = 0; i < params.size( ); i++ ) {
if ( i == 0 ) {
clob = this.getCLOB( (String) params.get( i ), con );
pstmt.setClob( ( i + 1 ), clob );
}
else {
int j = Integer.parseInt( (String) params.get( i ) );
pstmt.setInt( i + 1, j );
}
}
}
boolean flg = pstmt.execute( );
pstmt.close( );
}
catch ( SQLException ex ) {
throw new DBException( "Unable to execute the SQL Query in clobExecute()"
+ "method of DBBroker class of given status "
+ " : " + ex.getMessage( ) );
}
catch ( Exception ex ) {
throw new DBException( "Exception thrown in clobExecute() method of "
+ "DBBroker class of given status : "
+ ex.getMessage( ) );
}
finally {
try {
if ( pstmt != null ) {
pstmt.close( );
}
if ( clob != null ) {
freeCLOB( clob );
}
connector.releaseConnection( con ); }
catch ( Exception ex ) {
throw new DBException( "Unable to release the connection of given "
+ "status : " + ex.getMessage( ) );
}
}
}
private void freeCLOB( CLOB clob ) throws DBException {
try {
clob.freeTemporary( );
}
catch ( SQLException sqlexp ) {
throw new DBException( sqlexp.getMessage( ) );
}
}
}
|