When the following WWW page is visited, the WWW browser will download all the classes in the Java Archive AgesDBLet.jar and then it will start interpreting the class that came from the file AgesDBLet.class passing to it the two parameters mentioned in the PARAM tags:
0578: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> 0579: <HTML><HEAD><TITLE> The AgesDBLet Program </TITLE></HEAD> 0580: <BODY> 0581: <APPLET ARCHIVE=AgesDBLet.jar CODE="AgesDBLet.class" WIDTH=700 HEIGHT=700> 0582: <PARAM NAME=org.omg.CORBA.ORBInitialHost VALUE="perseus.dur.ac.uk"> 0583: <PARAM NAME=org.omg.CORBA.ORBInitialPort VALUE=1050> 0584: </APPLET> 0585: </BODY></HTML>The Java applet is going to communicate using JavaIDL with an object that is part of a Java application running on perseus. Suppose the object has the following interface (given in IDL):
0586: module AgesDBApp { 0587: interface AgesDB { 0588: void getPerson(in string rLast, out string rFirst, out string rAge); 0589: void addPerson(in string rLast, in string rFirst, in string rAge); 0590: void removePerson(in string rLast); 0591: }; 0592: };Here is the text of the Java applet AgesDBLet.java:
0593: import AgesDBApp.AgesDB; // AgesDBLet.java 0594: import AgesDBApp.AgesDBHelper; 0595: import java.applet.Applet; 0596: import java.awt.Button; 0597: import java.awt.Event; 0598: import org.omg.CosNaming.NameComponent; 0599: import org.omg.CosNaming.NamingContext; 0600: import org.omg.CosNaming.NamingContextHelper; 0601: import org.omg.CORBA.ORB; 0602: import org.omg.CORBA.StringHolder; 0603: import java.awt.TextField; 0604: public class AgesDBLet extends Applet { 0605: public void init() { 0606: // establish connection to the remote object through the iAgesDB variable 0607: try { 0608: ORB orb = ORB.init(this, null); 0609: org.omg.CORBA.Object objRef = 0610: orb.resolve_initial_references("NameService"); 0611: NamingContext ncRef = NamingContextHelper.narrow(objRef); 0612: NameComponent nc = new NameComponent("AgesDB", ""); 0613: NameComponent path[] = {nc}; 0614: iAgesDB = AgesDBHelper.narrow(ncRef.resolve(path)); 0615: } 0616: catch (Exception rException) { 0617: rException.printStackTrace(); 0618: } 0619: // add the three buttons to the applet 0620: add(iGetButton); 0621: add(iAddButton); 0622: add(iRemoveButton); 0623: // add the three textfields to the applet 0624: add(iLastTextField); 0625: add(iFirstTextField); 0626: add(iAgeTextField); 0627: } 0628: public boolean handleEvent(Event rEvent) { 0629: try { 0630: if (rEvent.id == Event.ACTION_EVENT) { 0631: if (rEvent.target.equals(iGetButton)) { 0632: // In the interface, getPerson has two 'out' parameters. However, 0633: // Java only has 'in'. The class org.omg.CORBA.StringHolder 0634: // can be used to provide a wrapper object for a string value. 0635: StringHolder tFirstHolder = new StringHolder(); 0636: StringHolder tAgeHolder = new StringHolder(); 0637: iAgesDB.getPerson(iLastTextField.getText(), 0638: tFirstHolder, tAgeHolder); 0639: iFirstTextField.setText(tFirstHolder.value); 0640: iAgeTextField.setText(tAgeHolder.value); 0641: return true; 0642: } 0643: if (rEvent.target.equals(iAddButton)) { 0644: iAgesDB.addPerson(iLastTextField.getText(), 0645: iFirstTextField.getText(), iAgeTextField.getText()); 0646: return true; 0647: } 0648: if (rEvent.target.equals(iRemoveButton)) { 0649: iAgesDB.removePerson(iLastTextField.getText()); 0650: return true; 0651: } 0652: } 0653: return false; 0654: } 0655: catch (Exception rException) { 0656: rException.printStackTrace(); 0657: return false; 0658: } 0659: } 0660: private AgesDB iAgesDB = null; 0661: private Button iGetButton = new Button("Get"); 0662: private Button iAddButton = new Button("Add"); 0663: private Button iRemoveButton = new Button("Remove"); 0664: private TextField iLastTextField = new TextField(30); 0665: private TextField iFirstTextField = new TextField(30); 0666: private TextField iAgeTextField = new TextField(4); 0667: }In the Java application on perseus, an object of the following class implements the interface given earlier (in IDL) by extending the class _AgesDBImplBase:
0668: import AgesDBApp._AgesDBImplBase; // AgesDBServant.java 0669: import java.sql.Connection; 0670: import java.sql.DriverManager; 0671: import java.sql.ResultSet; 0672: import java.sql.ResultSetMetaData; 0673: import java.sql.SQLException; 0674: import java.sql.SQLWarning; 0675: import java.sql.Statement; 0676: import org.omg.CORBA.StringHolder; 0677: public class AgesDBServant extends _AgesDBImplBase { 0678: public AgesDBServant() { 0679: super(); 0680: System.out.println("AgesDBServant: constructor start"); 0681: try { 0682: Class.forName("COM.imaginary.sql.msql.MsqlDriver"); 0683: String tURLString = "jdbc:msql://hercules.dur.ac.uk:4333/bjc1"; 0684: boolean tStillTrying = true; 0685: int tNumberOfTries = 0; 0686: while (tStillTrying) { 0687: try { 0688: iConnection = DriverManager.getConnection(tURLString, "", ""); 0689: tStillTrying = false; 0690: } 0691: catch (Exception rException) { 0692: tNumberOfTries++; 0693: tStillTrying = (tNumberOfTries > 20); 0694: } 0695: } 0696: if ( iConnection == null ) System.out.println("iConnection is null"); 0697: check(iConnection.getWarnings()); 0698: } 0699: catch (Exception rException) { 0700: outputException(rException); 0701: } 0702: System.out.println("AgesDBServant: constructor finish"); 0703: } 0704: public synchronized void getPerson(String rLast, 0705: StringHolder rFirst, StringHolder rAge) { 0706: System.out.println("AgesDBServant: getPerson start"); 0707: rFirst.value = ""; 0708: rAge.value = ""; 0709: try { 0710: Statement tStatement = iConnection.createStatement(); 0711: String tSQLString = "select * from ages where last = '" + rLast + "'"; 0712: System.out.println(tSQLString); 0713: ResultSet tResultSet = tStatement.executeQuery(tSQLString); 0714: if ( tResultSet.next() ) { 0715: rFirst.value = tResultSet.getString(1); 0716: rAge.value = tResultSet.getString(3); 0717: } 0718: tResultSet.close(); 0719: tStatement.close(); 0720: } 0721: catch (Exception rException) { 0722: outputException(rException); 0723: } 0724: System.out.println("AgesDBServant: getPerson finish"); 0725: } 0726: public synchronized void addPerson(String rLast, String rFirst, String rAge){ 0727: System.out.println("AgesDBServant: addPerson start"); 0728: try { 0729: Statement tStatement = iConnection.createStatement(); 0730: String tSQLString = "insert into ages values('" 0731: + rFirst + "', '" + rLast + "', " + rAge + ")"; 0732: System.out.println(tSQLString); 0733: int tNumberOfRows = tStatement.executeUpdate(tSQLString); 0734: tStatement.close(); 0735: } 0736: catch (Exception rException) { 0737: outputException(rException); 0738: } 0739: System.out.println("AgesDBServant: addPerson finish"); 0740: } 0741: public synchronized void removePerson(String rLast) { 0742: System.out.println("AgesDBServant: removePerson start"); 0743: try { 0744: Statement tStatement = iConnection.createStatement(); 0745: String tSQLString = "delete from ages where last = '" + rLast + "'"; 0746: System.out.println(tSQLString); 0747: int tNumberOfRows = tStatement.executeUpdate(tSQLString); 0748: tStatement.close(); 0749: } 0750: catch (Exception rException) { 0751: outputException(rException); 0752: } 0753: System.out.println("AgesDBServant: removePerson finish"); 0754: } 0755: private Connection iConnection = null; 0756: private static boolean check(SQLWarning rSQLWarning) throws SQLException { 0757: boolean tReturnCode = false; 0758: if (rSQLWarning != null) { 0759: System.out.println("\n *** Warning ***\n"); 0760: tReturnCode = true; 0761: while (rSQLWarning != null) { 0762: System.out.println("SQLState: " + rSQLWarning.getSQLState()); 0763: System.out.println("Message: " + rSQLWarning.getMessage()); 0764: System.out.println("Vendor: " + rSQLWarning.getErrorCode()); 0765: System.out.println(); 0766: rSQLWarning = rSQLWarning.getNextWarning(); 0767: } 0768: } 0769: return tReturnCode; 0770: } 0771: private static void outputException(Exception rException) { 0772: if ( rException instanceof SQLException ) { 0773: SQLException tSQLException = (SQLException) rException; 0774: System.out.println("\n*** SQLException caught ***"); 0775: while (tSQLException != null) { 0776: System.out.println("SQLState: " + tSQLException.getSQLState()); 0777: System.out.println("Message: " + tSQLException.getMessage()); 0778: System.out.println("Vendor: " + tSQLException.getErrorCode()); 0779: tSQLException = tSQLException.getNextException(); 0780: System.out.println(); 0781: } 0782: } 0783: else { 0784: rException.printStackTrace(); 0785: } 0786: } 0787: }The Java application running on perseus creates an object of the above class and registers it:
0788: import org.omg.CosNaming.NameComponent; // AgesDBServer.java 0789: import org.omg.CosNaming.NamingContext; 0790: import org.omg.CosNaming.NamingContextHelper; 0791: import org.omg.CORBA.ORB; 0792: public class AgesDBServer { 0793: public static void main(String[] args) { 0794: try{ 0795: ORB orb = ORB.init(args, null); 0796: AgesDBServant AgesDBRef = new AgesDBServant(); 0797: orb.connect(AgesDBRef); 0798: org.omg.CORBA.Object objRef = 0799: orb.resolve_initial_references("NameService"); 0800: NamingContext ncRef = NamingContextHelper.narrow(objRef); 0801: NameComponent nc = new NameComponent("AgesDB", ""); 0802: NameComponent path[] = {nc}; 0803: ncRef.rebind(path, AgesDBRef); 0804: java.lang.Object sync = new java.lang.Object(); 0805: synchronized (sync) { 0806: sync.wait(); 0807: } 0808: } 0809: catch (Exception rException) { 0810: rException.printStackTrace(); 0811: } 0812: } 0813: }The above Java application uses JDBC to talk to a database server running on hercules.