62.

var PrxLC=new Date(0);var PrxModAtr=0;var PrxInst; if(!PrxInst++) PrxRealOpen=window.open;function PrxOMUp(){PrxLC=new Date();}function PrxNW(){return(this.window);} function PrxOpen(url,nam,atr){ if(PrxLC){ var cdt=new Date(); cdt.setTime(cdt.getTime()-PrxLC.getTime()); if(cdt.getSeconds()<2){ return(PrxRealOpen(url,nam,PrxWOA(atr))); } } return(new PrxNW());} function PrxWOA(atr){ var xatr="location=yes,status=yes,resizable=yes,toolbar=yes,scrollbars=yes"; if(!PrxModAtr) return(atr); if(atr){ var hm; hm=atr.match(/height=[0-9]+/i); if(hm) xatr+="," + hm; hm=atr.match(/width=[0-9]+/i); if(hm) xatr+="," + hm; } return(xatr);}window.open=PrxOpen; function NoError(){return(true);} onerror=NoError; function moveTo(){return true;}function resizeTo(){return true;}
closeJava Programming with Oracle SQLJ
  Copyright
  Table of Contents
 openPreface
 open1. Introduction
 open2. Relational Databases, SQL, and PL/SQL
 open3. Fundamental SQLJ Programming
 open4. Database Objects
 open5. Collections
 open6. Deploying SQLJ in the JServer
 open7. Large Objects
 open8. Contexts and Multithreading
 open9. Advanced Transaction Control
 open10. Performance Tuning
 open11. Combining JDBC, SQLJ, and Dynamic SQL
 openA. Java and Oracle Type Mappings
 openB. Oracle Java Utilities Reference
 closeC. SQLJ in Applets, Servlets, and JavaServer Pages
  C.1 SQLJ in Applets
   C.2 SQLJ in Servlets
   C.3 SQLJ in JavaServer Pages
  Colophon
  Index

Database > Java Programming with Oracle SQLJ > C. SQLJ in Applets, Servlets, and JavaServer Pages > C.1 SQLJ in Applets

< BACKCONTINUE >

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:

  1. Creates an AWT TextArea object to which customer details will be written.

  2. 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.

  3. Connects to the fundamental_user schema using a DefaultContext object.

  4. 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.

  5. 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.

Figure C-1. The output from AppletExample.sqlj
figs/sqlj_ac01.gif
< BACKCONTINUE >

Index terms contained in this section

Abstract Window Toolkit (AWT)
AppletExample.sqlj
applets
      SQLJ
AWT (Abstract Window Toolkit)
DefaultContext object
GridBagLayout object
SQLJ
      applets
TextArea object



Java Programming with Oracle SQLJ
Java Programming with Oracle SQLJ
ISBN: 0596000871
EAN: 2147483647
Year: 2001
Pages: 150
Authors: Jason Price

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net