SQL Exceptions


The java.sql.SQLException class extends the general java.lang.Exception class that provides extra information about a database error. Each SQLException provides several kinds of information:
Password (args[0]) =       Table (args[1]) =

           
try {
  String database = "65.52.222.73:1521/cdb1";
  OracleDataSource ods = new OracleDataSource( );
  ods.setURL     ( "jdbc:oracle:thin:@" + database );
  ods.setUser    ( "C##user.id" );
  ods.setPassword( args[0].trim( ) );
  Connection conn = ods.getConnection( );
  System.out.println( "Successfully connect to Oracle.\n" );

  Statement  stmt = conn.createStatement( );
  String      cmd = "select * from " + args[1].trim( );
  System.out.println( cmd + "\n" );
  ResultSet  rset = stmt.executeQuery( cmd );
  // Iterate through the result and print the employees.
  while ( rset.next( ) )
    System.out.println (
      rset.getInt( 1 ) + " " + rset.getString( 2 ) );
}
catch( SQLException ex ) {
  ex.printStackTrace( );
  while ( ( ex = ex.getNextException( ) ) != null )
    // while more exceptions
    ex.printStackTrace( );
}

If you have serveral errors during the execution of a transaction, you can chain them all together in this class. This is useful when you have exceptions that you want to let the user to know about, but you do not want to stop processing. The class SQLWarning provides information on database access warnings. Warnings are silently chained to the object whose method caused it to be reported.