FileDocCategorySizeDatePackage
TestNullValues.javaAPI DocExample3034Thu Nov 30 17:46:14 GMT 2000None

TestNullValues.java

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

public class TestNullValues {
 Connection conn;
 public TestNullValues() {
  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 TestNullValues().process();
 }

 public void process() throws IOException, SQLException {
  double     a_double     = 0;
  Double     a_Double     = null;
  BigDecimal a_BigDecimal = null;
  long       a_long       = 0;
  Long       a_Long       = null;
  BigInteger a_BigInteger = null;
  String     a_String     = null;
  Timestamp  a_Timestamp  = 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++;
    a_String     = rslt.getString(1);
    if (rslt.wasNull()) System.out.println("rslt.getString(1) was null");
    System.out.println("a_String     = " + a_String);
    System.out.println(" ");

    a_BigDecimal = rslt.getBigDecimal(2);
    if (rslt.wasNull()) System.out.println("rslt.getBigDecimal(2) was null");
    System.out.println("a_BigDecimal = " + a_BigDecimal);
    System.out.println(" ");

    a_double     = rslt.getDouble(2);
    if (rslt.wasNull()) System.out.println("rslt.getDouble(2) was null");
    System.out.println("a_double     = " + a_double);
    System.out.println(" ");

    a_Double     = new Double(rslt.getDouble(2));
    if (rslt.wasNull()) System.out.println("rslt.getDouble(2) was null");
    System.out.println("a_Double     = " + a_Double);
    System.out.println(" ");

    a_long       = rslt.getLong(2);
    if (rslt.wasNull()) System.out.println("rslt.getLong(2) was null");
    System.out.println("a_long       = " + a_long);
    System.out.println(" ");

    a_Long       = new Long(rslt.getLong(2));
    if (rslt.wasNull()) System.out.println("rslt.getLong(2) was null");
    System.out.println("a_Long       = " + a_Long);
    System.out.println(" ");

    a_Timestamp  = rslt.getTimestamp(3);
    if (rslt.wasNull()) System.out.println("rslt.getTimestamp(3) was null");
    System.out.println("a_Timestamp  = " + a_Timestamp);
   }
  }
  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();
 }
}