FileDocCategorySizeDatePackage
HandlingNullValues1.javaAPI DocExample2029Sat Jun 23 17:02:58 BST 2001None

HandlingNullValues1.java

import java.io.*;
import java.math.*;
import java.sql.*;
import java.text.*;

public class HandlingNullValues1 {
  Connection conn;

  public HandlingNullValues1() {
    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();
    }
  }

  public static void main(String[] args) 
   throws Exception, IOException {
    new HandlingNullValues1().process();
  }

  public void process() throws IOException, SQLException {
    BigDecimal aBigDecimal = null;
    String     aString     = null;
    Timestamp  aTimestamp  = null;
    int        rows        = 0;
    ResultSet  rslt        = null;
    Statement  stmt        = null;
    try {
      stmt = conn.createStatement();
      rslt = stmt.executeQuery(
       "select to_char( NULL ), " +
       "       to_date( NULL ), " + 
       "       to_number( NULL ) " +
       "from   sys.dual");
      if (rslt.next()) {
        rows++;
        aString     = rslt.getString(1);
        aTimestamp  = rslt.getTimestamp(2);
        aBigDecimal = rslt.getBigDecimal(3);

        System.out.println("a String     = " + aString);
        System.out.println("a Timestamp  = " + aTimestamp);
        System.out.println("a BigDecimal = " + aBigDecimal);
      }
      rslt.close();
      rslt = null;
      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) { }
    }
  }

  protected void finalize() 
   throws Throwable {
    if (conn != null) 
      try { conn.close(); } catch (SQLException ignore) { }
    super.finalize();
  }
}