Database Reference
In-Depth Information
We will assure that we have a connection to
appver
, which we can use. We test whether
appVerConn
is
null
, a reference to the connection we are using to converse with
appver
. If it is
null
, one of two
problems exist: either we haven't connected yet as
appver
(call
getAppConnections()
), or we have
overwritten our connection to
appver
, so it is no longer available to execute the update,
putAppConnections()
. The solution is to always call
putAppConnections()
, if needed, before getting one of
the connection strings from the list for use in the application.
It is a trivial matter to convert a Java object, as long as it implements
Serializable
, into a byte array.
However, it does look at bit daunting at first. Drilling down into this code, we write our object into an
ObjectOutputStream
,
oout
. The
ObjectOutputStream
is tied directly to a
ByteArrayOutputStream
,
baos
.
After we write our object, we flush
oout
and close it, assuring that our entire object is delivered to
baos
. At
that point, we call the
toByteArray()
method of
baos
to get our object out as a byte array.
Listing 10-24.
Store Connection Strings List in Oracle, putAppConnections()
public static void putAppConnections(){
OracleCallableStatement stmt = null;
try {
if( null == appVerConn )
{
if( null == conn ) {
System.out.println( "Call getAppConnections to establish " +
"connection to AppVer first, " +
"else can not putAppConnections!" );
} else {
System.out.println( "Connection to AppVer overwritten - " +
"can not putAppConnections!" );
}
return;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream( baos );
oout.writeObject( appClass )
;
oout.flush()
;
oout.close()
;
byte[] appClassBytes = baos.toByteArray()
;
baos.close();
baos = new ByteArrayOutputStream();
oout = new ObjectOutputStream( baos );
oout.writeObject(
connsHash
);
oout.flush();
oout.close();
byte[]
connsHashBytes
= baos.toByteArray();
baos.close();
stmt = ( OracleCallableStatement )conn.prepareCall(
"{? = call appsec.appsec_public_pkg.
f_set_decrypt_conns
(?,?)}" );
stmt.registerOutParameter( 1, OracleTypes.VARCHAR );
stmt.setBytes( 2, appClassBytes )
;
stmt.setBytes( 3, connsHashBytes )
;
stmt.executeUpdate();