C.1 SQLJ in Applets
An applet is a program that may be downloaded and executed in a web browser, such as Netscape Navigator or Internet Explorer, and run using the Java Virtual Machine that comes with the browser. You can also run applets from the command line, which is helpful for debugging and testing purposes. This section shows a simple applet, named AppletExample.sqlj, that contains SQLJ statements, and describes how to compile and run this applet from the command line. For an introduction to applets, I recommend the book Learning Java by Patrick Niemeyer and Joshua Peck (O'Reilly).
AppletExample.sqlj (Example C-1) uses the Abstract Window Toolkit (AWT) which contains windows, graphics, and user interface classes to display customer details retrieved from the customers table. AppletExample.sqlj performs the following steps:
-
Creates an AWT TextArea object to which customer details will be written.
-
Creates an AWT GridBagLayout object into which the TextArea object is placed. A GridBagLayout object allows you to position UI components relative to one another. The GridBagLayout will be visible in the applet when it is executed.
-
Connects to the fundamental_user schema using a DefaultContext object.
-
Creates and populates an iterator object named cust_iterator, which stores the id, first_name, last_name, dob, and phone column values retrieved from the customers table.
-
Populates the TextArea object with the customer details.
Example C-1. AppletExample.sqlj
/* 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); } } }
You can compile the applet using the sqlj command-line utility, and run it using the java command-line utility. For example:
sqlj AppletExample.sqlj java AppletExample
Figure C-1 shows the results of running the applet from the command line using the java command. You can also compile and run the applet using JDeveloper.