Listing 1
import java.sql.*;
import java.util.*;
import oracle.sqlj.runtime.Oracle;
#sql iterator MyIterator (String, double) ;
public class MyClass
{
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("user", "scott");
props.put("password", "tiger");
props.put("server", "MJR");
Driver ociDriver = (Driver)
Class.forName("weblogic.jdbc.oci.Driver").newInstance();
DriverManager.registerDriver(ociDriver);
Connection conn = ociDriver.connect("jdbc:weblogic:oracle",
props);
Oracle.connect(conn);
MyIterator myIterator = null;
String ename = null;
double sal = 0;
#sql myIterator = { select ENAME, SAL from EMP order by ENAME };
while (true) {
#sql { FETCH :myIterator INTO :ename, :sal };
if (myIterator.endFetch()) break;
System.out.println(ename + " " + sal);
}
myIterator.close();
Oracle.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Listing 2
\>java -classpath %CLASSPATH%;. MyClass
Starting Loading jDriver/Oracle .....
ADAMS 1100.0
ALLEN 1600.0
BLAKE 2850.0
CLARK 2450.0
FORD 3000.0
JAMES 950.0
JONES 2975.0
KING 5000.0
MARTIN 1250.0
MILLER 1300.0
SCOTT 3000.0
SMITH 800.0
TURNER 1500.0
WARD 1250.0
Listing 3
import java.sql.*;
import java.util.*;
import java.io.Serializable;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.DataSource;
import oracle.sqlj.runtime.Oracle;
#sql context MyContext;
#sql iterator MyIterator (String, double) ;
public class MyBean implements SessionBean {
private SessionContext ctx;
public void ejbActivate() {}
public void ejbCreate () throws CreateException {}
public void ejbRemove() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;v
}
public Vector query() {
MyContext myContext = null;
try {
myContext = init();
MyIterator myIterator = null;
Vector v = new Vector();
String ename = null;
double sal = 0;
#sql [myContext] myIterator =
{ select ENAME, SAL from EMP order by ENAME };
while (true) {
#sql { FETCH :myIterator INTO :ename, :sal };
if (myIterator.endFetch()) break;
v.addElement(new Emp(ename, sal));
}
myIterator.close();
return v;
} catch (SQLException sqle) {
throw new EJBException(sqle);
}
finally
{
cleanup(myContext);
}
}
private MyContext init()
throws SQLException
{
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
DataSource ds = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/demoPool");
MyContext myContext = new
MyContext(Oracle.getConnection(ds.getConnection()));
return myContext;
} catch(NamingException ne) {
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
throw new EJBException(ne);
}
}
}
private void cleanup(MyContext myContext) {
try {
myContext.close();
Oracle.close();
} catch (Exception e) {
throw new EJBException (e);
}
}
}
Listing 4
<-- Run the sqlj precompiler -->
<target name="sqlj">
<echo message="Using ${sqljlib1}"/>
<echo message="Using ${sqljlib2}"/>
<exec dir="${source}" executable="sqlj">
<arg line="MyBean.sqlj"/>
<arg line="-status"/>
<arg line="-compile=false"/>
<arg line="-d=${build}"/>
<env key="CLASSPATH"
path="${java.class.path}:${sqljlib1}:${sqljlib2}"/>
</exec>
</target>