FileDocCategorySizeDatePackage
TableViewer.javaAPI DocExample2242Wed Apr 05 11:25:42 BST 2000None

TableViewer.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import java.sql.*;
import java.util.StringTokenizer;

public class TableViewer {

  /* Note for the Online Version - 
     The book uses a set of non-functional Oracle placeholders.
     For the downloadable sample code, we have changed them to point
     to a more standard data source.

     Also, be aware that some versions of the JDK (including 1.15 on NT)
     do not seem to support ResultSet.getMetaData().	
   */

  final static String jdbcURL = "jdbc:oracle:oci8:@net8name";
  final static String jdbcDriver = "oracle.jdbc.driver.OracleDriver";
  final static String table = "CUSTOMERS";

  public static void main(java.lang.String[] args) {
 
    System.out.println("--- Table Viewer ---");
   
    try {
      Class.forName(jdbcDriver);
      Connection con = DriverManager.getConnection(jdbcURL, "ssdata", "ssite");
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * FROM "+ table);

      ResultSetMetaData rsmd = rs.getMetaData();
      int columnCount = rsmd.getColumnCount();
      for(int col = 1; col <= columnCount; col++) {
        System.out.print(rsmd.getColumnLabel(col));
        System.out.print(" (" + rsmd.getColumnTypeName(col)+")");
        if(col < columnCount) 
          System.out.print(", ");
      }
      System.out.println();
  
      while(rs.next()) {
        for(int col = 1; col <= columnCount; col++) {
          System.out.print(rs.getString(col));
          if(col < columnCount) 
            System.out.print(", ");
        } 
        System.out.println();
      }  
 
      rs.close();
      stmt.close();
      con.close();
    }
    catch (ClassNotFoundException e) {
      System.out.println("Unable to load database driver class");
    }
    catch (SQLException e) {
      System.out.println("SQL Exception: " + e.getMessage());
    }
  }
}