Access the Oracle server through SQL J of DB2
Today I saw a book about Oracle SQLJ, so I want to give it a try. However, I only installed the Oracle thin client on Ubuntu 14.04. There is no SQLJ, so the sqlj thought of using the IBM-db2, it was successful.
1. Run runtime12ee. jar runtime12.jar translator. jar into the corresponding directory on the Oracle server.
I put it in $ ORACLE_HOME/sqlj/lib, where $ ORACLE_HOME is the installation directory of the thin client.
/Opt/ora11g/instantclient_11_2/
2. Add $ ORACLE_HOME/sqlj/lib/* To CLASSPATH.
3. Add the-compile = false-C-classpath = $ ORACLE_HOME/sqlj/lib/* option when converting with the sqlj program.
Iv. Instances
/* Emp. sqlj source code
Sqlj-compile = false-C-classpath = $ ORACLE_HOME/sqlj/lib/* Emp. sqlj
Javac Emp. java
Java Emp
*/
Import java. SQL. SQLException;/* the Java class required for importing */
Import sqlj. runtime. *; // supported during Java runtime
Import sqlj. runtime. ref .*;
Import oracle. sqlj. runtime. *; // Oracle Extension
# SQL iterator MyIter (String empno, String ename );
Class Emp/* main class */
/* Define the iterator processing result set */
{
Public static void main (String args [])
{
Try {
/* Connect to the Oracle database. Here the Thin driver is used */
Oracle. connect ("jdbc: oracle: thin: @ 192.168.0.110: 1521: orcl", "scott", "tiger ");
Emp st = new Emp ();
St. runExample ();
} Catch (SQLException e ){
System. err. println ("Error running the example:" + e );
} Finally {
Try {
Oracle. close ();
} Catch (SQLException e ){}
}
}
Void runExample () throws SQLException {/* define the runExample function to obtain and process the result */
MyIter iter;/* declare an instance of the iterator class */
# SQL iter = {select EMPNO, ENAME from emp};/* execute the embedded SQL statement and return the result to iterator */
While (iter. next ()){
/* Loop traversal of each record in the result and output */
System. out. print ("EMPNO =" + iter. empno () + "");
System. out. println ("ENAME =" + iter. ename ());
}
}
}
Run:
$ Sqlj-compile = false-C-classpath = $ ORACLE_HOME/sqlj/lib/* Emp. sqlj
$ Javac Emp. java
$ Java Emp
EMPNO = 7369 ENAME = SMITH
EMPNO = 7499 ENAME = ALLEN
EMPNO = 7521 ENAME = WARD
EMPNO = 7566 ENAME = JONES
EMPNO = 7654 ENAME = MARTIN
EMPNO = 7698 ENAME = BLAKE
EMPNO = 7782 ENAME = CLARK
EMPNO = 7788 ENAME = SCOTT
EMPNO = 7839 ENAME = KING
EMPNO = 7844 ENAME = TURNER
EMPNO = 7876 ENAME = ADAMS
EMPNO = 7900 ENAME = JAMES
EMPNO = 7902 ENAME = FORD
EMPNO = 7934 ENAME = MILLER