Java source in oracle

Solo disponible en BuenasTareas
  • Páginas : 7 (1690 palabras )
  • Descarga(s) : 0
  • Publicado : 2 de diciembre de 2010
Leer documento completo
Vista previa del texto
Java Stored Procedure in Oracle, Database Interaction

(Page 1 of 5 )

In this article, we will examine working with Java Stored Procedures in Oracle database.
The entire code has been tested with only Oracle 10g with version 10.2. It would also support any successive versions after Oracle 8i. But, I didn’t personally check all of those versions.
Stored Procedures being written in Java?I contributed a very long series (about 20 parts) that covered database interaction with PL/SQL. I received good feedback from several readers from all parts of the world. Indeed with their request, I am still extending the same series (but with different titles).
I already introduced an article which explains what a stored procedure is. We also examined how to create, execute and dropstored procedures in an Oracle database. Now, we will examine a similar concept but turn a bit to Java. To follow this article, one should have at least a basic idea of Java (at least its syntax, compilation, and so on) together with a bit of PL/SQL knowledge. For a complete list of my PL/SQL articles, try searching through “igrep.”
Even though I am working on Oracle 10g (v10.2) now, right fromversion 8i, the Oracle database includes a fully functional Java Virtual Machine called “Oracle JVM.” Oracle and Sun have a long-term relationship for their applications and platform interoperability. One of the important technologies which came out of their growing relationship is “Java stored procedures.”
With that flexibility, suddenly many Java developers became something like “Oracle Developersfor Java” or “Java Oracle developers.” Not only Java stored procedures, but Oracle has further integrated very tightly with almost all of the latest J2EE specification (including, SQL, JSP, EJB and so on). In fact, Oracle developed its own application server (Oracle Application Server or simple “10gAS”) to integrate with their other suites.
With Java stored procedures, developers have theability to harness the power of Java when building database applications. Almost the entire core Java API can be used to develop stored procedures. And further they will be served as if they are “native” Oracle stored procedures (generally written in PL/SQL).
In this article, we will examine how to create our own Java stored procedure, right from scratch.
Java Stored Procedure in Oracle, DatabaseInteraction - A Simple Java Class

(Page 2 of 5 )
One should remember that Java stored procedures are still Java classes, but stored as Oracle schema objects. They will be made accessible to Oracle SQL and PL/SQL through call specifications. Call specifications, as we will see, are simply PL/SQL declarations that 'wrap' methods of Java stored in the database. Call specifications, in otherwords, work as mediators (or interfaces).
Another important issue to consider is that Java methods must be “public” and they must be “static” if they are to be used as stored procedures inside an Oracle database.
JDeveloper is the product from Oracle, which seems to be a famous IDE for Java developers who need Oracle based Java stored procedures to develop and deploy very easily. You can write,compile, and even unit test your Java code before moving it into the Oracle database. But in this article, you work with a simple notepad by saving the following code as “Employee.java”.
The following listing displays a simple Java class called “Employee”. For now, it contains a single method to “insert” an employee record into the database.
import java.sql.*;
import oracle.jdbc.*;
public classEmployee {
//Add an employee to the database.
public static void addEmp(int empno, String ename,
float sal, int deptno) {
System.out.println("Creating new employee...");
try {
Connection conn =
DriverManager.getConnection("jdbc:default:connection:");
String sql =
"INSERT INTO emp " +
"(empno,ename,sal,deptno)...
tracking img