«^»
4.5. A Java application that uses JDBC

Here is a program that is divided into the above steps. The program is written so that it accesses either a database called bjc1 that is attached to an mSQL database server running on a computer called hercules:

or it uses the JDBC-ODBC bridge to access a database called cestria:

The choice depends on a value passed on the command line used to start the program:

0330: import java.sql.Connection;                                 // BothTion.java
0331: import java.io.DataInputStream;
0332: import java.sql.DriverManager;
0333: import java.sql.ResultSet;
0334: import java.sql.ResultSetMetaData;
0335: import java.sql.SQLException;
0336: import java.sql.SQLWarning;
0337: import java.sql.Statement;
0338: public class BothTion {
0339:    public static void main(String[] args) {
0340:       try {
0341:          DataInputStream stdin = new DataInputStream(System.in);
0342:          Connection tConnection = null;
0343:          String tURLString;
0344:          if ( args[0].equals("bjc1") ) {
0345:             Class.forName("COM.imaginary.sql.msql.MsqlDriver");
0346:             tURLString = "jdbc:msql://hercules.dur.ac.uk:4333/bjc1";
0347:          }
0348:          else {
0349:             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
0350:             tURLString = "jdbc:odbc:cestria";
0351:          }
0352:          boolean tStillTrying = true;
0353:          int tNumberOfTries = 0;
0354:          while (tStillTrying) {
0355:             try {
0356:                tConnection = DriverManager.getConnection(tURLString, "", "");
0357:                tStillTrying = false;
0358:             }
0359:             catch (Exception rException) {
0360:                tNumberOfTries++;
0361:                tStillTrying = (tNumberOfTries > 20);
0362:             }
0363:          }
0364:          if ( tConnection == null )  System.out.println("tConnection is null");
0365:          check(tConnection.getWarnings());
0366:          Statement tStatement = tConnection.createStatement();
0367:          boolean tMoreSQLRequests = true;
0368:          while (tMoreSQLRequests) {
0369:             System.out.print("> ");
0370:             String tSQLString = stdin.readLine();
0371:             try {
0372:                if ( tSQLString.startsWith("select") ) {
0373:                   ResultSet tResultSet = tStatement.executeQuery(tSQLString);
0374:                   showResultSet(tResultSet);
0375:                   tResultSet.close();
0376:                }
0377:                else if ( tSQLString.startsWith("quit") ) {
0378:                   tMoreSQLRequests = false;
0379:                }
0380:                else {
0381:                   // assume line contains a SQL insert, update or delete
0382:                   int tNumberOfRows = tStatement.executeUpdate(tSQLString);
0383:                   }
0384:                }
0385:             catch (Exception rException) {
0386:                outputException(rException);
0387:             }
0388:          }
0389:          tStatement.close();
0390:          tConnection.close();
0391:       }
0392:       catch (Exception rException) {
0393:          outputException(rException);
0394:       }
0395:    }
0396:    private static void showResultSet(ResultSet rResultSet) throws SQLException {
0397:       // Get the ResultSetMetaData.  This will be used for
0398:       // the column headings
0399:       ResultSetMetaData tResultSetMetaData = rResultSet.getMetaData();
0400:       // Get the number of columns in the result set
0401:       int tNumCols = tResultSetMetaData.getColumnCount();
0402:       for (int tColNum=1; tColNum<=tNumCols; tColNum++) {
0403:          if (tColNum>1) System.out.print(", ");
0404:          System.out.print(tResultSetMetaData.getColumnLabel(tColNum));
0405:       }
0406:       System.out.println();
0407:       // Display data, fetching until end of the result set
0408:       boolean tMoreRows = rResultSet.next();
0409:       while (tMoreRows) {
0410:          // Loop through each column, getting the
0411:          // column data and displaying
0412:          for (int tColNum=1; tColNum<=tNumCols; tColNum++) {
0413:             if (tColNum>1) System.out.print(", ");
0414:             System.out.print(rResultSet.getString(tColNum));
0415:          }
0416:          System.out.println();
0417:          // Fetch the next result set row
0418:          tMoreRows = rResultSet.next();
0419:       }
0420:    }
0421:    private static boolean check(SQLWarning rSQLWarning) throws SQLException {
0422:       boolean tReturnCode = false;
0423:       if (rSQLWarning != null) {
0424:          System.out.println("\n *** Warning ***");
0425:          tReturnCode = true;
0426:          while (rSQLWarning != null) {
0427:             System.out.println("SQLState: " + rSQLWarning.getSQLState());
0428:             System.out.println("Message:  " + rSQLWarning.getMessage());
0429:             System.out.println("Vendor:   " + rSQLWarning.getErrorCode());
0430:             System.out.println();
0431:             rSQLWarning = rSQLWarning.getNextWarning();
0432:          }
0433:       }
0434:       return tReturnCode;
0435:    }
0436:    private static void outputException(Exception rException) {
0437:       if ( rException instanceof SQLException ) {
0438:          SQLException tSQLException = (SQLException) rException;
0439:          System.out.println("\n*** SQLException caught ***");
0440:          while (tSQLException != null) {
0441:             System.out.println("SQLState: " + tSQLException.getSQLState());
0442:             System.out.println("Message:  " + tSQLException.getMessage());
0443:             System.out.println("Vendor:   " + tSQLException.getErrorCode());
0444:             tSQLException = tSQLException.getNextException();
0445:             System.out.println();
0446:          }
0447:       }
0448:       else {
0449:          rException.printStackTrace();
0450:       }
0451:    }
0452: }