We will use the following Java applet AgesLet.java to communicate with an mSQL database server using the JDBC API:
0453: import java.applet.Applet; // AgesLet.java
0454: import java.awt.Button;
0455: import java.sql.Connection;
0456: import java.sql.DriverManager;
0457: import java.awt.Event;
0458: import java.sql.ResultSet;
0459: import java.sql.ResultSetMetaData;
0460: import java.sql.SQLException;
0461: import java.sql.SQLWarning;
0462: import java.sql.Statement;
0463: import java.awt.TextArea;
0464: import java.awt.TextField;
0465: public class AgesLet extends Applet {
0466: public void init() {
0467: add(iQueryTextField);
0468: add(iQueryButton);
0469: add(iResultsTextArea);
0470: try {
0471: Class.forName("COM.imaginary.sql.msql.MsqlDriver");
0472: String tURLString = "jdbc:msql://hercules.dur.ac.uk:4333/bjc1";
0473: boolean tStillTrying = true;
0474: int tNumberOfTries = 0;
0475: while (tStillTrying) {
0476: try {
0477: iConnection = DriverManager.getConnection(tURLString, "", "");
0478: tStillTrying = false;
0479: }
0480: catch (Exception rException) {
0481: tNumberOfTries++;
0482: tStillTrying = (tNumberOfTries > 20);
0483: }
0484: }
0485: if ( iConnection == null) iResultsTextArea.append("is null\n");
0486: check(iConnection.getWarnings());
0487: }
0488: catch (Exception rException) {
0489: outputException(rException);
0490: }
0491: }
0492: public void destroy() {
0493: try {
0494: iConnection.close();
0495: }
0496: catch (Exception rException) {
0497: outputException(rException);
0498: }
0499: }
0500: public boolean handleEvent(Event rEvent) {
0501: if (rEvent.target.equals(iQueryButton) && rEvent.id==Event.ACTION_EVENT) {
0502: try {
0503: String tSQLString = iQueryTextField.getText();
0504: Statement tStatement = iConnection.createStatement();
0505: if ( tSQLString.startsWith("select") ) {
0506: ResultSet tResultSet = tStatement.executeQuery(tSQLString);
0507: displayResultSet(tResultSet);
0508: tResultSet.close();
0509: }
0510: else {
0511: int tNumberOfRows = tStatement.executeUpdate(tSQLString);
0512: }
0513: tStatement.close();
0514: }
0515: catch (Exception rException) {
0516: outputException(rException);
0517: }
0518: iResultsTextArea.append("--------------------------------\n");
0519: return true;
0520: }
0521: return false;
0522: }
0523: private TextField iQueryTextField = new TextField(80);
0524: private Button iQueryButton = new Button("click here");
0525: private TextArea iResultsTextArea = new TextArea(20, 80);
0526: private Connection iConnection = null;
0527: private void displayResultSet(ResultSet rResultSet) throws SQLException {
0528: ResultSetMetaData tResultSetMetaData = rResultSet.getMetaData();
0529: int tNumCols = tResultSetMetaData.getColumnCount();
0530: boolean tMoreRows = rResultSet.next();
0531: while (tMoreRows) {
0532: for (int tColNum=1; tColNum<=tNumCols; tColNum++) {
0533: iResultsTextArea.append(rResultSet.getString(tColNum));
0534: iResultsTextArea.append(" ");
0535: }
0536: iResultsTextArea.append("\n");
0537: tMoreRows = rResultSet.next();
0538: }
0539: }
0540: private static boolean check(SQLWarning rSQLWarning) throws SQLException {
0541: boolean tReturnCode = false;
0542: if (rSQLWarning != null) {
0543: System.out.println("\n *** Warning ***");
0544: tReturnCode = true;
0545: while (rSQLWarning != null) {
0546: System.out.println("SQLState: " + rSQLWarning.getSQLState());
0547: System.out.println("Message: " + rSQLWarning.getMessage());
0548: System.out.println("Vendor: " + rSQLWarning.getErrorCode());
0549: System.out.println();
0550: rSQLWarning = rSQLWarning.getNextWarning();
0551: }
0552: }
0553: return tReturnCode;
0554: }
0555: private static void outputException(Exception rException) {
0556: if ( rException instanceof SQLException ) {
0557: SQLException tSQLException = (SQLException) rException;
0558: System.out.println("\n*** SQLException caught ***");
0559: while (tSQLException != null) {
0560: System.out.println("SQLState: " + tSQLException.getSQLState());
0561: System.out.println("Message: " + tSQLException.getMessage());
0562: System.out.println("Vendor: " + tSQLException.getErrorCode());
0563: tSQLException = tSQLException.getNextException();
0564: System.out.println();
0565: }
0566: }
0567: else {
0568: rException.printStackTrace();
0569: }
0570: }
0571: }
This file can be compiled using the command:
javac AgesLet.javaThe compiler produces a file called AgesLet.class.
You will need to put the classes/COM directory of the mSQL-JDBC 1.0a4 distribution into the current directory. On a Unix system, you can do this by a command like:
cp -pr /usr/local/utils/mSQL/JDBC/1.0a4/classes/COM .This creates a subdirectory called COM in the current directory.
You now need to produce a Java Archive. This is Java's way of packaging up a lot of files to produce one file. It is done using JDK 1.1.x's jar command at the Unix/MS-DOS prompt:
jar cf AgesLet.jar AgesLet.class COMThis command will produce a jar file in the file AgesLet.jar.