Database Reference
In-Depth Information
if( stmt == null ) …
In this case, the complaint is that we are trying to use something in our logic that may not have been
initialized. Java wants to make informed decisions and sees this as a potential for confusion, even at
compile time.
Calling Oracle Database from Java
Within the Java method named getOracleTime() , in Listing 4-1, are calls to four classes that are used for
interfacing with Oracle database: OracleDriver , Connection , Statement , and ResultSet . The following
sections describe each of those four classes and what they do.
OracleDriver
The first call is to load the OracleDriver in the Java database connectivity (JDBC) drivers registry (list).
(You can use multiple drivers; e.g. Oracle database, SQL Server, and MySQL. Each would need to be
registered.) I show several options for registering the OracleDriver . They are all commented for reasons
I'll explain in a second.
//Class.forName( "oracle.jdbc.driver.OracleDriver" );
//new oracle.jdbc.OracleDriver();
//Connection conn = new OracleDriver().defaultConnection();
The first option uses a call to instantiate an OracleDriver class, sight unseen, by using the
Class.forName() call. The Class.forName() method is a way to instantiate an object by simply stating its
fully qualified name. By instantiating the OracleDriver this way, it is automatically loaded into the
drivers registry. This is the standard practice for loading the OracleDriver , and you will see it frequently.
The second option is to instantiate a new OracleDriver() , which automatically gets loaded in the
drivers registry. Lastly, we have an option to create a new instance of OracleDriver (again automatically
loaded in the drivers registry) and in the same line of code, use the OracleDriver to get a Connection
object.
Now, let me tell you why all of these invocations are commented. There are two reasons. The first
has to do with the fact that we are running this code as a Java stored procedure on the Oracle database.
As part of the Oracle session, the Oracle JVM automatically loads and registers the OracleDriver .
The second reason, but one that is less dependable, is that from Java version 1.6 on, the
OracleDriver is loaded automatically, as needed (with ojdbc6.jar in the CLASSPATH and a hint from the
connection string, e.g. “ jdbc: oracle :thin ”). This is less dependable, because you as the developer have
the option of compiling and running this code with whatever version of Java you have available. You are
only required to have JDK 1.5 or later to follow this topic, so we will continue to load the OracleDriver in
any code that will run on your client workstation.
Connection and Statement
Next in Listing 4-1, we establish a connection to Oracle database (repeated in the following). We define
the connection based on the default, " jdbc:default:connection. " We will place this Java code into the
Oracle database, so we do not need to specify an Oracle server or Oracle listener port number, nor do we
need to identify an Oracle user or password. By default, the Java stored in the database runs as the user
associated with the schema where the Java is loaded. Because we are creating this Java structure using
the appsec user, it will run with the default roles and privileges of appsec .
 
Search WWH ::




Custom Search