FileDocCategorySizeDatePackage
TestMetaData.javaAPI DocExample5361Sun Jun 24 18:59:44 BST 2001None

TestMetaData

public class TestMetaData extends Object

Fields Summary
Connection
conn
Constructors Summary
public TestMetaData()

    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
       "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  
Methods Summary
protected voidfinalize()

    if (conn != null) 
      try { conn.close(); } catch (SQLException ignore) { }
    super.finalize();
  
private java.lang.StringformatColumn(java.lang.Object object, java.lang.String className, int displaySize)

    String value  = "";

    if      (className.equals("java.lang.String")) {
      if (object != null) {
        value = rpad((String)object, displaySize, ' ");
      }
      else {
        value = rpad(value, displaySize, ' ");
      }
    }
    else if (className.equals("java.math.BigDecimal")) {
      if (object != null) {
        BigDecimal n = (BigDecimal)object;
        value = lpad(n.toString(), 9, ' ");
      }
      else {
        value = rpad(value, 9, ' ");
      }
    }
    else if (className.equals("java.sql.Timestamp")) {
      if (object != null) {
        Timestamp ts = (Timestamp)object;
        value = rpad(ts.toString(), 21, ' ");
      }
      else {
        value = rpad(value, 21, ' ");
      }
    }
    else {
      System.err.println("Unsupported class name: " + className);
    }
    return value + " ";
  
private java.lang.StringformatHeading(java.lang.String heading, java.lang.String className, int displaySize)

    int    length = displaySize;
    String value  = "";

    if (heading != null) {
      value = heading;
      if      (className.equals("java.lang.String")) {
      }
      else if (className.equals("java.math.BigDecimal")) {
        length = 9;
      }
      else if (className.equals("java.sql.Timestamp")) {
        length = 21;
      }
      else {
        System.err.println("Unsupported class name: " + className);
      }
    }
    return rpad(value, length, ' ") + " ";
  
private java.lang.Stringlpad(java.lang.String in, int length, char pad)

    StringBuffer out   = new StringBuffer(length);
    int          least = in.length();

    if (least > length) 
      least = length;
    out.append(in.substring(0, least));
    int          fill  = length - out.length();
    for (int i=0;i < fill;i++) { 
      out.insert(0, pad);
    }
    return out.toString();
  
public static voidmain(java.lang.String[] args)

    TestMetaData tmd = new TestMetaData();

    tmd.process(
     "select to_char( NULL )   a_char,  " + 
     "       to_date( NULL )   a_date,  " +
     "       to_number( NULL ) a_number " + 
     "from   sys.dual");

    tmd.process(
     "select 'ABCDEFG' a_char,     " +
     "       sysdate   a_date,     " + 
     "       1         an_integer, " +
     "       1.1       a_float     " +
     "from   sys.dual");

    tmd.process(
     "delete PERSON " +
     "where  1 = 0");
  
public voidprocess(java.lang.String sql)

    int               columns = 0;
    int               i       = 0;
    int               rows    = 0;
    ResultSet         rslt    = null;
    ResultSetMetaData meta    = null;
    Statement         stmt    = null;

    try {
      stmt = conn.createStatement();
      if (stmt.execute(sql)) {
        rslt = stmt.getResultSet();
        meta = rslt.getMetaData();
        columns = meta.getColumnCount();
        while (rslt.next()) {
          rows++;
          if (rows == 1) {
            for (i = 1;i <= columns;i++) {
              System.out.print(
               formatHeading(
               meta.getColumnLabel(i), 
               meta.getColumnClassName(i), 
               meta.getColumnDisplaySize(i)));  
            }
            System.out.println("");
          }
          for (i = 1;i <= columns;i++) {
            System.out.print(
             formatColumn(
             rslt.getObject(i), 
             meta.getColumnClassName(i), 
             meta.getColumnDisplaySize(i)));  
          }
          System.out.println("");
        }
        System.out.println("");
        rslt.close();
        rslt = null;
        meta = null;
      }
      else {
        rows = stmt.getUpdateCount();
        System.out.println(Integer.toString(rows) + " rows affected");
        System.out.println("");
      }
      stmt.close();
      stmt = null;
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (rslt != null) 
        try { rslt.close(); } catch (SQLException ignore) { }
      if (stmt != null) 
        try { stmt.close(); } catch (SQLException ignore) { }
    }
  
private java.lang.Stringrpad(java.lang.String in, int length, char pad)

    StringBuffer out   = new StringBuffer(length);
    int          least = in.length();

    if (least > length) 
      least = length;
    out.append(in.substring(0, least));
    int          fill  = length - out.length();
    for (int i=0;i < fill;i++) { 
      out.append(pad);
    }
    return out.toString();