Oracle8i JDBC Developer's Guide and Reference Release 8.1.5 A64685-01 |
|
This section presents an example of a signed applet which uses the JDBC Thin driver to connect to and query a database. The code used in the applet was created with Oracle JDeveloper and complies with JDK 1.1.2 and JDBC 1.22. Signed applets are also browser-specific; the applet defined in this section works with the Netscape 4.x browser.
The applet displays a user interface that lets you connect to a local or a remote database, depending on whether you press the "Local" or "Remote" button. The applet queries the selected database for the contents of a specified row and displays the results.
If you want to try this example on your own system, you must provide this information:
http://developer.netscape.com/docs/manuals/signedobj/capabilities/contents.htm
Follow the instructions for obtaining a certificate and downloading the classes. The example in this section requires the Capabilities classes Principle.class
, Privilege.class
, PrivilegeManager.class
, and PrivilegeTable.class
.
In the applet code, replace the following strings:
local database connect string
> with the connect string for the local database. For example:
"jdbc:oracle:thin:@myServer.us.oracle.com:1521:orcl", "scott","tiger"
select on row of local table
> with a SQL SELECT
statement on a row in a table in the local database. For example:
SELECT * FROM EMP WHERE ENAME = 'Mary'
remote database connect string
> with the connect string for the remote database. For example:
"jdbc:oracle:thin:@yourServer.us.oracle.com:1521:orcl", "scott","tiger"
select on row of remote table
> with a SQL SELECT
statement on a table in the remote database. For example:
SELECT * FROM EMP WHERE ENAME = 'Bob'
This applet uses only the Java AWT
components and JDBC.
// Title: JDBC Test Applet // line 1 // Description:Sample JDK 1.1 Applet using the // ORACLE JDBC Thin Driver package JDBCApplet; import java.awt.*; // line 6 import java.awt.event.*; import java.applet.*; import java.sql.*; import borland.jbcl.control.*; import netscape.security.*; // line 12 public class MainApplet extends Applet { boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); Panel panel1 = new Panel(); Label labelTitle = new Label(); Panel panel2 = new Panel(); BorderLayout borderLayout2 = new BorderLayout(); TextArea txtArResults = new TextArea(); Button button1 = new Button(); BorderLayout borderLayout3 = new BorderLayout(); Panel panel3 = new Panel(); BorderLayout borderLayout4 = new BorderLayout(); Label statusBar1 = new Label(); Button button2 = new Button(); // Get a parameter value // line 28 public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // line 32 // Construct the applet public MainApplet() { } // Initialize the applet line 37 public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } try { PrivilegeManager.enablePrivilege("UniversalConnect"); PrivilegeManager.enablePrivilege("UniversalListen"); PrivilegeManager.enablePrivilege("UniversalAccept"); } catch (Exception e) { e.printStackTrace(); } } // Component initialization line 49 public void jbInit() throws Exception{ this.setBounds(new Rectangle(0, 0, 400, 400)); panel1.setBackground(Color.lightGray); panel1.setLayout(borderLayout3); this.setSize(new Dimension(372, 373)); labelTitle.setBackground(Color.lightGray); labelTitle.setFont(new Font("Dialog", 0, 12)); labelTitle.setAlignment(1); labelTitle.setText("Oracle Thin JDBC Driver Sample Applet"); button1.setLabel("Local"); panel3.setBackground(Color.lightGray); statusBar1.setBackground(Color.lightGray); statusBar1.setText("Ready"); button2.setLabel("Remote"); button2.addActionListener(new MainApplet_button2_actionAdapter(this)); panel3.setLayout(borderLayout4); button1.addActionListener(new MainApplet_button1_actionAdapter(this)); panel2.setLayout(borderLayout2); this.setLayout(borderLayout1); this.add(panel1, BorderLayout.NORTH); panel1.add(button1, BorderLayout.WEST); panel1.add(labelTitle, BorderLayout.CENTER); panel1.add(button2, BorderLayout.EAST); this.add(panel2, BorderLayout.CENTER); panel2.add(txtArResults, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); panel3.add(statusBar1, BorderLayout.NORTH); } //Start the applet line 79 public void start() { } //Stop the applet public void stop() { } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } //Main method static public void main(String[] args) { MainApplet applet = new MainApplet(); applet.isStandalone = true; Frame frame = new Frame(); frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.pack(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void button1_actionPerformed(ActionEvent e) { // // Handler for "Local" Button. // // Here is where we connect to local database line 121 StringBuffer b = new StringBuffer (); try { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver ()); b.append ("DriverManager.registerDriver\r\n"); } catch (SQLException oe) { statusBar1.setText("registerDriver: Caught SQLException"); } catch (ClassNotFoundException oe) { statusBar1.setText("registerDriver: Caught ClassNotFoundException"); } int numRows = 0; try { statusBar1.setText("Executing Query on Local Database ..."); Connection conn = DriverManager.getConnection ( "jdbc:oracle:thin:<local database connect string
>"); b.append ("[DriverManager.getConnection] \r\n"); Statement stmt = conn.createStatement (); b.append ("[conn.createStatement] \r\n"); ResultSet rset = stmt.executeQuery ("<select on row of
local table
>"); b.append ("[stmt.executeQuery] \r\n"); b.append("SQL> <select on row of local table
>\r\n\n"); b.append("DSCr\n--------------------------------------\r\n"); while (rset.next ()) { String ename = rset.getString (1); b.append (ename); b.append ("\r\n"); numRows++; } // [end while rset.next() loop] statusBar1.setText("Query Done."); } catch (SQLException SQLE) { statusBar1.setText ("Caught SQLException!"); SQLE.printStackTrace(); } finally { b.append("\r\n"); b.append(String.valueOf(numRows) + " rows selected.\r\n"); txtArResults.setText( b.toString ()); } // End JDBC Code line 165 } void button2_actionPerformed(ActionEvent e) { // // Handler for the "Remote" Button line 170 // StringBuffer b = new StringBuffer (); try { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver ()); b.append ("DriverManager.registerDriver\r\n"); } catch (SQLException oe) { statusBar1.setText("registerDriver: Caught SQLException"); } catch (ClassNotFoundException oe) { statusBar1.setText("registerDriver: Caught ClassNotFoundException"); } int numRows = 0; // line 183 try { statusBar1.setText("Executing Query on Remote Database ..."); try { PrivilegeManager.enablePrivilege("UniversalConnect"); b.append ("enablePrivilege(UniversalConnect)\r\n"); PrivilegeManager.enablePrivilege("UniversalListen"); b.append ("enablePrivilege(UniversalListen)\r\n"); PrivilegeManager.enablePrivilege("UniversalAccept"); b.append ("enablePrivilege(UniversalAccept)\r\n"); Connection conn = DriverManager.getConnection ( "jdbc:oracle:thin:<remote database connect string
>" ); b.append ("DriverManager.getConnection\r\n"); Statement stmt = conn.createStatement (); b.append ("conn.createStatement\r\n"); ResultSet rset = stmt.executeQuery ("<select on row
of remote table
>"); b.append ("stmt.executeQuery\r\n"); b.append("SQL> <select on row of remote table
>\r\n\n"); b.append("ENAME\r\n----------\r\n"); while (rset.next ()) { String ename = rset.getString (1); b.append (ename); b.append ("\r\n"); numRows++; } // [end while rset.next() loop] statusBar1.setText("Query Done."); } catch (Exception oe) { oe.printStackTrace(); } } catch (SQLException SQLE) { statusBar1.setText("Caught SQLException!"); SQLE.printStackTrace(); } finally { b.append("\r\n"); b.append(String.valueOf(numRows) + " rows selected.\r\n"); txtArResults.setText( b.toString ()); } // End JDBC Code for Button2 line 256 } } // line 260 class MainApplet_button1_actionAdapter implements java.awt.event.ActionListener { MainApplet adaptee; MainApplet_button1_actionAdapter(MainApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.button1_actionPerformed(e); } } // line 273 class MainApplet_button2_actionAdapter implements java.awt.event.ActionListener { MainApplet adaptee; MainApplet_button2_actionAdapter(MainApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.button2_actionPerformed(e); } }
Import the needed files.
Set up the graphics for the GUI which will include two buttons and a text area to display the output.
Request privileges to connect to the host other than the one from which the applet was downloaded.
Initialize the components of the applet. These components include the format and layout of the GUI and the GUI buttons and text area.
Connect to the local database. To do this, register the driver with the DriverManager.registerDriver()
method and connect to the database with DriverManager.getConnection()
. Connect with the server URL, port number, SID, user name, and password.
Connect to the remote database.
Test that the applet has privileges on the remote database. If it does, then connect to the database and execute SQL statements.
Code to set up events and callbacks for the buttons.