0% found this document useful (0 votes)
7 views

Writing The Java Class

Uploaded by

Pankaj Gaikwad
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Writing The Java Class

Uploaded by

Pankaj Gaikwad
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Writing the Java Class

The beauty of this first step is that it really has little to do with the Oracle database.
You simply develop your Java classes using your favorite IDE, such as Oracle's
JDeveloper. Java methods must be public and they must be static if they are to be
used as stored procedures.
You can write, compile, and even unit test your Java code before moving it into the
Oracle database. In fact, for all but trivial applications, this is the preferred method
because it will allow you to take advantage of your IDE's features, such as debugging
and code generation. If you would prefer to compile your Java classes with Oracle's
JVM, the loadjava utility, discussed later, can do this for you.
The following listing displays a simple Java class called EmpManager. For now, it
contains a single method to insert an emp record into the database.
import java.sql.*;
import oracle.jdbc.*;

public class EmpManager {

//Add an employee to the database.


public static void addEmp(int emp_id, String emp_f_name,
String emp_l_name,float emp_salary, int dept_id) {

System.out.println("Creating new employee...");

try {
Connection conn =
DriverManager.getConnection("jdbc:default:connection:");

String sql =
"INSERT INTO emp " +
"(emp_id,emp_f_name,emp_l_name,emp_salary,dept_id) " +
"VALUES(?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,emp_id);
pstmt.setString(2,emp_f_name);
pstmt.setString(3,emp_l_name);
pstmt.setFloat(4,emp_salary);
pstmt.setInt(5,dept_id);
pstmt.executeUpdate();
pstmt.close();
}
catch(SQLException e) {
System.err.println("ERROR! Adding Employee: "
+ e.getMessage());
}
}
}
There is nothing out of the ordinary here. Well, almost nothing. In this method, the
database connection URL is "jdbc:default:connection:". When writing Java that will
execute inside the Oracle database, you can take advantage of a special server-side
JDBC driver. This driver uses the user's default connection and provides the fastest
access to the database.

#2. Loading Classes


Our Java class is to become a full-fledged schema object, so it needs to be moved
into the database. Oracle provides a command-line utility called loadjava to
accomplish this task. The loadjava utility essentially provides an interface for SQL
CREATE JAVA statements, which also may be used to migrate Java-related files to the
database.
Because we've yet to compile EmpManager.java, we'll ask loadjava to do this as part
of the loading process. This is achieved by specifying the -resolve attribute on the
utility.
$ loadjava -u scott/tiger -v -resolve EmpManager.java
In addition to the -resolve attribute, the -v instructs the utility to include verbose
feedback, and the -u specifies the database user and password. Because we asked
loadjava to compile the source file, both the source and class files become members
of the SCOTT schema.
We can verify the status of the compilation and load with a simple query against
USER_OBJECTS. If done correctly, the status is 'VALID'.
SELECT object_name, object_type, status
FROM user_objects WHERE object_type LIKE 'JAVA%';

object_name object_type status

EmpManager JAVA CLASS VALID


EmpManager JAVA SOURCE VALID
Conversely, if compilation fails, errors can be examined through the USER_ERRORS
view.
If you choose to compile with an IDE, simply load the resulting class file. Then, the
source can reside in version control on the file system. The loadjava utility accepts
files with the extensions .sqlj (a sqlj source file), .properties, .ser, .jar, and .zip. In the
case of .jar and .zip files, Oracle automatically extracts and stores each member as
individual schema objects.
Before we move on, there's one more critical component to the load process that
merits discussion: the Oracle JVM resolver. Typically, a JVM uses a classpath to locate
Java classes that your program depends upon to run correctly. When storing Java in
the database, a resolver accomplishes this.

You might also like