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:
- A string describing the error. This is used as the Java exception message, available via the method
getMessage
.
- An “SQLstate” string, which follows either the XOPEN
SQLstate
conventions or the SQL99 conventions.
- An integer error code that is specific to each vendor.
Normally this will be the actual error code returned by the underlying database.
- A chain to a next exception.
This can be used to provide additional error information.
|
|
try {
String database = "20.185.147.112:1521/xe";
OracleDataSource ods = new OracleDataSource( );
ods.setURL ( "jdbc:oracle:thin:@" + database );
ods.setUser ( "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.