Commit 817f35b8 by O'Reilly Media, Inc.

Initial commit

parents
9780596000875
\ No newline at end of file
## Example files for the title:
# Java Programming with Oracle SQLJ, by Jason Price
[![Java Programming with Oracle SQLJ, by Jason Price](http://akamaicovers.oreilly.com/images/9780596000875/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596000871//)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
logo.png

2.98 KB

/*@lineinfo:filename=AppletExample*//*@lineinfo:user-code*//*@lineinfo:1^1*//*
AppletExample.sqlj illustrates how to include
SQLJ statements in an applet.
*/
// import the applet and AWT classes
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
// import the SQLJ classes
import java.sql.*;
import oracle.sqlj.runtime.Oracle;
import sqlj.runtime.ref.DefaultContext;
public class AppletExample extends Applet
{
// declare an iterator class
/*@lineinfo:generated-code*//*@lineinfo:21^3*/
// ************************************************************
// SQLJ iterator declaration:
// ************************************************************
private static class CustIteratorClass
extends sqlj.runtime.ref.ResultSetIterImpl
implements sqlj.runtime.NamedIterator
{
public CustIteratorClass(sqlj.runtime.profile.RTResultSet resultSet)
throws java.sql.SQLException
{
super(resultSet);
idNdx = findColumn("id");
first_nameNdx = findColumn("first_name");
last_nameNdx = findColumn("last_name");
dobNdx = findColumn("dob");
phoneNdx = findColumn("phone");
}
public int id()
throws java.sql.SQLException
{
return resultSet.getIntNoNull(idNdx);
}
private int idNdx;
public String first_name()
throws java.sql.SQLException
{
return resultSet.getString(first_nameNdx);
}
private int first_nameNdx;
public String last_name()
throws java.sql.SQLException
{
return resultSet.getString(last_nameNdx);
}
private int last_nameNdx;
public java.sql.Date dob()
throws java.sql.SQLException
{
return resultSet.getDate(dobNdx);
}
private int dobNdx;
public String phone()
throws java.sql.SQLException
{
return resultSet.getString(phoneNdx);
}
private int phoneNdx;
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:27^3*/
// create a TextArea object to write the customer details
TextArea output;
// create a connection context
DefaultContext conn_context;
public void init() {
// create a grid (gridbag) to display the customer details
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
constraints.gridheight = 10;
constraints.gridwidth = 5;
output = new TextArea(30, 30);
gridbag.setConstraints(output, constraints);
add(output);
constraints.gridwidth = 5;
// retrieve and display the customer details
try {
conn_context = Oracle.connect(
"jdbc:oracle:thin:@localhost:1521:orcl",
"fundamental_user",
"fundamental_password"
);
CustIteratorClass cust_iterator;
/*@lineinfo:generated-code*//*@lineinfo:59^7*/
// ************************************************************
// #sql [conn_context] cust_iterator = { SELECT
// id, first_name, last_name, dob, phone
// FROM
// customers
// };
// ************************************************************
{
sqlj.runtime.ConnectionContext __sJT_connCtx = conn_context;
if (__sJT_connCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX();
sqlj.runtime.ExecutionContext __sJT_execCtx = __sJT_connCtx.getExecutionContext();
if (__sJT_execCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX();
synchronized (__sJT_execCtx) {
sqlj.runtime.profile.RTStatement __sJT_stmt = __sJT_execCtx.registerStatement(__sJT_connCtx, AppletExample_SJProfileKeys.getKey(0), 0);
try
{
sqlj.runtime.profile.RTResultSet __sJT_result = __sJT_execCtx.executeQuery();
cust_iterator = new CustIteratorClass(__sJT_result);
}
finally
{
__sJT_execCtx.releaseStatement();
}
}
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:64^7*/
while (cust_iterator.next()) {
output.append("Id = " + cust_iterator.id() + "\n");
output.append("First name = " + cust_iterator.first_name() +
"\n");
output.append("Last name = " + cust_iterator.last_name() + "\n");
output.append("DOB = " + cust_iterator.dob() + "\n");
output.append("Phone = " + cust_iterator.phone() + "\n");
} // end of while
} catch (SQLException e) {
output.append("SQLException " + e);
}
}
// used in the main method to close the applet
private static Applet appletReference = null;
public static void main(String[] args) {
// create an Applet object
Applet appletExample = new AppletExample();
// create a frame to display the content
Frame frame = new Frame();
frame.setTitle("Customers");
frame.setSize(300, 600);
frame.add("Center", appletExample);
// initialize the applet, this causes the customer
// details to be displayed in the applet
appletExample.init();
// show the frame in the applet
frame.show();
// allow the user to stop the applet
// using the UI
appletReference = appletExample;
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
appletReference.stop();
System.exit(0);
}
}
);
}
public void stop() {
try {
conn_context.close();
} catch (SQLException e) {
System.out.println("SQLException " + e);
}
}
}/*@lineinfo:generated-code*/class AppletExample_SJProfileKeys
{
private static AppletExample_SJProfileKeys inst = null;
public static java.lang.Object getKey(int keyNum)
throws java.sql.SQLException
{
if (inst == null)
{
inst = new AppletExample_SJProfileKeys();
}
return inst.keys[keyNum];
}
private final sqlj.runtime.profile.Loader loader = sqlj.runtime.RuntimeContext.getRuntime().getLoaderForClass(getClass());
private java.lang.Object[] keys;
private AppletExample_SJProfileKeys()
throws java.sql.SQLException
{
keys = new java.lang.Object[1];
keys[0] = DefaultContext.getProfileKey(loader, "AppletExample_SJProfile0");
}
}
/*
AppletExample.sqlj illustrates how to include
SQLJ statements in an applet.
*/
// import the applet and AWT classes
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
// import the SQLJ classes
import java.sql.*;
import oracle.sqlj.runtime.Oracle;
import sqlj.runtime.ref.DefaultContext;
public class AppletExample extends Applet
{
// declare an iterator class
#sql private static iterator CustIteratorClass (
int id,
String first_name,
String last_name,
java.sql.Date dob,
String phone
);
// create a TextArea object to write the customer details
TextArea output;
// create a connection context
DefaultContext conn_context;
public void init() {
// create a grid (gridbag) to display the customer details
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
constraints.gridheight = 10;
constraints.gridwidth = 5;
output = new TextArea(30, 30);
gridbag.setConstraints(output, constraints);
add(output);
constraints.gridwidth = 5;
// retrieve and display the customer details
try {
conn_context = Oracle.connect(
"jdbc:oracle:thin:@localhost:1521:orcl",
"fundamental_user",
"fundamental_password"
);
CustIteratorClass cust_iterator;
#sql [conn_context] cust_iterator = {
SELECT
id, first_name, last_name, dob, phone
FROM
customers
};
while (cust_iterator.next()) {
output.append("Id = " + cust_iterator.id() + "\n");
output.append("First name = " + cust_iterator.first_name() +
"\n");
output.append("Last name = " + cust_iterator.last_name() + "\n");
output.append("DOB = " + cust_iterator.dob() + "\n");
output.append("Phone = " + cust_iterator.phone() + "\n");
} // end of while
} catch (SQLException e) {
output.append("SQLException " + e);
}
}
// used in the main method to close the applet
private static Applet appletReference = null;
public static void main(String[] args) {
// create an Applet object
Applet appletExample = new AppletExample();
// create a frame to display the content
Frame frame = new Frame();
frame.setTitle("Customers");
frame.setSize(300, 600);
frame.add("Center", appletExample);
// initialize the applet, this causes the customer
// details to be displayed in the applet
appletExample.init();
// show the frame in the applet
frame.show();
// allow the user to stop the applet
// using the UI
appletReference = appletExample;
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
appletReference.stop();
System.exit(0);
}
}
);
}
public void stop() {
try {
conn_context.close();
} catch (SQLException e) {
System.out.println("SQLException " + e);
}
}
}
/*@lineinfo:filename=AppletExample*//*@lineinfo:user-code*//*@lineinfo:1^1*//*
AppletExample.sqlj illustrates how to include
SQLJ statements in an applet.
*/
// import the applet and AWT classes
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
// import the SQLJ classes
import java.sql.*;
import oracle.sqlj.runtime.Oracle;
import sqlj.runtime.ref.DefaultContext;
public class AppletExample extends Applet
{
// declare an iterator class
/*@lineinfo:generated-code*//*@lineinfo:21^3*/
// ************************************************************
// SQLJ iterator declaration:
// ************************************************************
private static class CustIteratorClass
extends sqlj.runtime.ref.ResultSetIterImpl
implements sqlj.runtime.NamedIterator
{
public CustIteratorClass(sqlj.runtime.profile.RTResultSet resultSet)
throws java.sql.SQLException
{
super(resultSet);
idNdx = findColumn("id");
first_nameNdx = findColumn("first_name");
last_nameNdx = findColumn("last_name");
dobNdx = findColumn("dob");
phoneNdx = findColumn("phone");
}
public int id()
throws java.sql.SQLException
{
return resultSet.getIntNoNull(idNdx);
}
private int idNdx;
public String first_name()
throws java.sql.SQLException
{
return resultSet.getString(first_nameNdx);
}
private int first_nameNdx;
public String last_name()
throws java.sql.SQLException
{
return resultSet.getString(last_nameNdx);
}
private int last_nameNdx;
public java.sql.Date dob()
throws java.sql.SQLException
{
return resultSet.getDate(dobNdx);
}
private int dobNdx;
public String phone()
throws java.sql.SQLException
{
return resultSet.getString(phoneNdx);
}
private int phoneNdx;
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:27^3*/
// create a TextArea object to write the customer details
TextArea output;
// create a connection context
DefaultContext conn_context;
public void init() {
// create a grid (gridbag) to display the customer details
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
constraints.gridheight = 10;
constraints.gridwidth = 5;
output = new TextArea(30, 30);
gridbag.setConstraints(output, constraints);
add(output);
constraints.gridwidth = 5;
// retrieve and display the customer details
try {
conn_context = Oracle.connect(
"jdbc:oracle:thin:@localhost:1521:orcl",
"fundamental_user",
"fundamental_password"
);
CustIteratorClass cust_iterator;
/*@lineinfo:generated-code*//*@lineinfo:59^7*/
// ************************************************************
// #sql [conn_context] cust_iterator = { SELECT
// id, first_name, last_name, dob, phone
// FROM
// customers
// };
// ************************************************************
{
sqlj.runtime.ConnectionContext __sJT_connCtx = conn_context;
if (__sJT_connCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX();
sqlj.runtime.ExecutionContext __sJT_execCtx = __sJT_connCtx.getExecutionContext();
if (__sJT_execCtx == null) sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX();
synchronized (__sJT_execCtx) {
sqlj.runtime.profile.RTStatement __sJT_stmt = __sJT_execCtx.registerStatement(__sJT_connCtx, AppletExample_SJProfileKeys.getKey(0), 0);
try
{
sqlj.runtime.profile.RTResultSet __sJT_result = __sJT_execCtx.executeQuery();
cust_iterator = new CustIteratorClass(__sJT_result);
}
finally
{
__sJT_execCtx.releaseStatement();
}
}
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:64^7*/
while (cust_iterator.next()) {
output.append("Id = " + cust_iterator.id() + "\n");
output.append("First name = " + cust_iterator.first_name() +
"\n");
output.append("Last name = " + cust_iterator.last_name() + "\n");
output.append("DOB = " + cust_iterator.dob() + "\n");
output.append("Phone = " + cust_iterator.phone() + "\n");
} // end of while
} catch (SQLException e) {
output.append("SQLException " + e);
}
}
// used in the main method to close the applet
private static Applet appletReference = null;
public static void main(String[] args) {
// create an Applet object
Applet appletExample = new AppletExample();
// create a frame to display the content
Frame frame = new Frame();
frame.setTitle("Customers");
frame.setSize(300, 600);
frame.add("Center", appletExample);
// initialize the applet, this causes the customer
// details to be displayed in the applet
appletExample.init();
// show the frame in the applet
frame.show();
// allow the user to stop the applet
// using the UI
appletReference = appletExample;
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
appletReference.stop();
System.exit(0);
}
}
);
}
public void stop() {
try {
conn_context.close();
} catch (SQLException e) {
System.out.println("SQLException " + e);
}
}
}/*@lineinfo:generated-code*/class AppletExample_SJProfileKeys
{
private static AppletExample_SJProfileKeys inst = null;
public static java.lang.Object getKey(int keyNum)
throws java.sql.SQLException
{
if (inst == null)
{
inst = new AppletExample_SJProfileKeys();
}
return inst.keys[keyNum];
}
private final sqlj.runtime.profile.Loader loader = sqlj.runtime.RuntimeContext.getRuntime().getLoaderForClass(getClass());
private java.lang.Object[] keys;
private AppletExample_SJProfileKeys()
throws java.sql.SQLException
{
keys = new java.lang.Object[1];
keys[0] = DefaultContext.getProfileKey(loader, "AppletExample_SJProfile0");
}
}
/*
AppletExample.sqlj illustrates how to include
SQLJ statements in an applet.
*/
// import the applet and AWT classes
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
// import the SQLJ classes
import java.sql.*;
import oracle.sqlj.runtime.Oracle;
import sqlj.runtime.ref.DefaultContext;