FileDocCategorySizeDatePackage
HandlingSQLExceptions.javaAPI DocExample1267Thu Apr 19 19:57:10 BST 2001None

HandlingSQLExceptions

public class HandlingSQLExceptions extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


  Class.forName("oracle.jdbc.driver.OracleDriver");
// or you can use:
//  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

  Connection conn = 
   DriverManager.getConnection(
    "jdbc:oracle:thin:@dssw2k01:1521:orcl","scott","tiger");
  System.out.println("Connection opened.");
  
  String    sql  = 
   "select 'Hello Thin driver tester '||USER||'!' result from xdual";
  Statement stmt = null;
  ResultSet rset = null;
  try {
   stmt = conn.createStatement();
   rset = stmt.executeQuery(sql);
   while(rset.next()) 
    System.out.println(rset.getString(1));
   rset.close();
   rset = null;
   stmt.close();
   stmt = null;
  }
  catch (SQLException e) {
   System.err.println("SQLException: " + e.getMessage().trim());
   System.err.println("in HandlingSQLExceptions while executing: ");
   System.err.println(sql);
  }
  finally {
   if (rset != null) try { rset.close(); } catch (SQLException ignore) { }
   if (stmt != null) try { stmt.close(); } catch (SQLException ignore) { }
  }
  conn.close();
  System.out.println("Connection closed.");