Database Reference
In-Depth Information
Listing 5-8. Static Initializer for Connection
private static Connection conn;
static {
try {
conn = new OracleDriver().defaultConnection();
} catch ( Exception x ) {}
}
The static modifier in front of an independent block of code is a static initializer—this is not a
method. Notice that we have to prepare to catch any exceptions that might be generated by instantiating
the Connection . We do not have to do anything with the possible exceptions (if there is an Exception ,
then our problems are more systemic than our code), but we have to establish the try / catch blocks to
make the code valid for compiling.
As a static block, it executes one time and exists in the class, outside of and without an instance of
the class.
Using One Code for Both Client and Server
For me, one of the most enjoyable aspects of coding in Java and Java stored procedures is the ability to
write code that operates on both sides of the client/server communication. This is especially helpful
when it comes to testing our code—we can, in many cases, test it all on our workstation prior to
deploying it to the Oracle database.
Some aspects of our code will only be executed on the client; for example the method to generate
the RSA key pair. Likewise, some portions of our code will normally only be executed on the Oracle
database, for example rebuilding our RSA public key from the artifacts. However for testing, we can
execute the methods destined for Oracle on our workstation.
Note Please refer to Appendix A for a list of all the methods of OracleJavaSecure and the indication of their
primary usage(client or Oracle Database or both).
In other instances, we will have to provide specific logic to permit our code to run in both
environments. One case in point is the Connection member. In the last section, I mentioned the static
initializer that will instantiate a Connection for use on the Oracle database. That code also runs on the
client, but has no effect—there is no local, default Oracle database for the code to connect to. So we
provide an additional method called setConnection() that will take a Connection from some client
application code and set the static Connection member to that Connection , as shown in Listing 5-9.
Listing 5-9. Setter Method for Client Connection, setConnection()
public static final void setConnection( Connection c ) {
conn = c;
}
We will have some additional examples of specific logic to run on either the client or the server,
which I will point out as we go along.
 
Search WWH ::




Custom Search