Brought to you by EarthWeb
ITKnowledge Logo Login Graphic 60-day trial version of Microsoft Project 98
60-day trial version of Microsoft Project 98
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Listing 10-2: An Interactive SQL Client.

import java.sql.*;
import java.io.*;
import java.util.*;
public class isql {
  static DataInputStream kbd = new DataInputStream(System.in);
  static String url = "jdbc:odbc:netbank";
  static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  static String login = "dba";
  static String passwd = "javabank";
  static Connection curConn = null;
  public static void main(String argv[]) throws IOException
  {
       String temp = "";
       System.out.println("Simple Java Isql, by Bernard Van Haecke, 1996.\n");
       System.out.print("Enter the url or [ENTER] for " + url + " : ");
       System.out.flush();
       temp = kbd.readLine();
      if (!temp.equals("")) url = temp;
       System.out.print("Enter the login or [ENTER] for " + login + " : ");
       System.out.flush();
       temp = kbd.readLine();
       if (!temp.equals("")) login = temp;
       System.out.print("Enter the passwd or [ENTER] for default : ");
       System.out.flush();
       temp = kbd.readLine();
       if (!temp.equals("")) passwd = temp;
       isql session = new isql();
  }
  public isql() throws IOException
  {
       try {
       Class.forName(driver);
       curConn = DriverManager.getConnection(url, login, passwd);
       checkForWarnings(curConn.getWarnings ());
       }
       catch(java.lang.Exception ex) {
       System.out.println("url : " + url);
       System.out.println("login : " + login);
       System.out.println("passwd : " + passwd);
       ex.printStackTrace();
       return;
       }
       processQueries();
       finalize();
  }
  protected void finalize()
  {
       try {
       curConn.close();
       }
       catch (SQLException ex) { }
  }
  private void processQueries() throws IOException
  {
       int i = 1;
       String temp = "";
       String query = "";
       String results = "";
       System.out.println("Type 'quit' on a blank line to exit, or 'go' to execute the query.");
       do {
            System.out.print(i + "> ");
            System.out.flush();
            temp = kbd.readLine();
            if (temp.equals("quit"))
                 break;
            if (temp.equals("go")) {
                 executeThisQuery(query);
                 i = 1;
                 query = " ";
            }
            else {
                 query = query + " " + temp;
                 i++;
            }
       } while (true);
  }
  private void executeThisQuery(String sqlText)
  {
       boolean resultSetIsAvailable;
       boolean moreResultsAvailable;
       int i = 0;
       int res=0;
       try {
            Statement curStmt = curConn.createStatement();
            resultSetIsAvailable = curStmt.execute(sqlText);
            ResultSet rs = null;
            for (moreResultsAvailable = true; moreResultsAvailable;)
            {
                 checkForWarnings(curConn.getWarnings());
                 if (resultSetIsAvailable)
                 {
                      if ((rs = curStmt.getResultSet()) != null)
                      {
                           // we have a resultset
                           checkForWarnings(curStmt.getWarnings());
                           ResultSetMetaData rsmd = rs.getMetaData();
                           int numCols = rsmd.getColumnCount();
                           // display column headers
                           for (i = 1; i <= numCols; i++)
                           {
                                if (i > 1) System.out.print(", ");
                                System.out.print(rsmd.
                                  getColumnLabel(i));
                           }
                           System.out.println("");
                           // step through the rows
                           while (rs.next())
                           {
                                // process the columns
                                for (i = 1; i <= numCols; i++)
                                {
                                  if (i > 1) System.out.print(", ");
                                          System.out.print(rs.
                                            getString(i));
                                     }
                                     System.out.println("");
                                 }
                           }
                      }
                      else
                      {
                           if ((res = curStmt.getUpdateCount()) != -1)
                           {
                                // we have an updatecount
                                System.out.println(res + " row(s) affected.");
                           }
                           // else no more results
                           else
                           {
                                moreResultsAvailable = false;
                           }
                     }
                     if (moreResultsAvailable)
                     {
                           resultSetIsAvailable = curStmt.getMoreResults();
                      }
                }
            if (rs != null) rs.close();
            curStmt.close();
       }
       catch (SQLException ex) {
            // Unexpected SQL exception.
            ex.printStackTrace ();
       }
       catch (java.lang.Exception ex) {
            // Got some other type of exception. Dump it.
            ex.printStackTrace ();
       }
  }
  private static void checkForWarnings (SQLWarning warn)
                 throws SQLException
  {
       while (warn != null) {
            System.out.println(warn);
            warn = warn.getNextWarning();
       }
  }
}

Simple ISQL Client Applet

Here is another ISQL client. It is different from the previous example because it runs as an applet. Anyone with a Java-enabled World Wide Web (WWW) browser can load the hypertext markup language (HTML) file containing the applet tag that calls this applet. The logic to execute the SQL statements is similar to the logic of the command-line ISQL example.

A 100-percent Java JDBC driver must be supplied with such an applet. The JDBC-ODBC Bridge, for example, would not work because it calls native methods to talk to ODBC. “All-Java” drivers are becoming available as more and more developers implement the JDBC Driver Application Programming Interface (API). Good examples are Sybase’s jConnect and Connect Software’s FastForward JDBC driver.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.