JDBC  «Prev  Next»
Lesson 3 Miscellaneous JDBC classes
ObjectiveDescribe several JDBC helper classes.

Miscellaneous JDBC classes

You may have discovered other JDBC classes and interfaces as you read the JDK documentation. Several utility classes add to the capabilities of JDBC.
The following three classes enable the developer to work with time and date as SQL objects. They are:
  1. java.sql.time
  2. java.sql.timestamp
  3. java.sql.date
This helps make date and time values independent of any particular database system and helps identify these values as being associated with SQL.

Constants

Constants, which are used to name the SQL data types, are in the java.sql.types class. These constants are mapped to integer values, which identify the standard SQL types. These values were established by the XOPEN.SQL standard. You will learn more about several unique SQL types in a later module.

Is the XOPEN.SQL standard still being used today?

The X/Open SQL standard, also known as the SQL Access Group standard, is no longer widely used today. The X/Open SQL standard was developed in the 1980s as a standard for SQL databases, with the goal of promoting interoperability between different database systems and applications. However, the standard was superseded by newer standards such as SQL-92, SQL:1999, and SQL:2003, which provided more comprehensive and up-to-date specifications for SQL.
Today, many database systems support the more recent SQL standards, which have been developed and maintained by organizations such as the International Organization for Standardization (ISO) and the American National Standards Institute (ANSI). These standards continue to evolve with new features and capabilities, and are widely used in both commercial and open source database systems. While the X/Open SQL standard is no longer in widespread use, its legacy can still be seen in the development of SQL and the ongoing effort to create more interoperable and standardized database systems.


SQL Specific Errors

There are also four classes that deal with SQL specific errors. These classes are:
  1. java.sql SQLException
  2. java.sql.SQLWarning
  3. java.sql.BatchUpdateException
  4. java.sql.DataTruncation

These classes all extend the standard java.lang.exception class and add SQL-related information. One of them, SQLException, is extended by the other three classes. An SQLException provides some information about the error, which includes:
  1. A description of the error in a String. This is the Java exception message.
  2. A String, SQLState, which describes the error in either XOPEN or in SQL99 terms, depending on the driver. These values are defined in the relevant specifications.
  3. A vendor-specific integer error code. This is usually the actual error code reported by the database itself.
  4. A chain to another exception. This can be used to provide more control over how and when exceptions are reported.
You should examine the Javadocs for more information about these classes. View the code below to see a sample of how SQLExceptions can be caught in a catch block.
catch (SQLException e) {
      System.out.println("SQLState: " + e.getSQLState() );
      System.out.println("Error Code: " + e.getErrorCode() );
      System.out.println("Exception Message: " + e.getMessage());
}


Not all errors should stop processing. There are warning messages that can be generated from a database, and they may not be severe enough to halt the program. JDBC deals with them through the SQLWarning class. These warnings can be generated on Connection, Statement, and ResultSet objects. If a warning error is raised by one of these JDBC objects, a java.sql.SQLWarning is created. This warning is attached to that object and available from getWarnings() methods on that object. If there are multiple SQLWarnings, they are chained together, and repeated invocations of getWarning() will expose them.
If the database unexpectedly truncates a data value, the DataWarning() exception is generated. If it occurs on a read it is treated as a warning and chained to the associated object. On a write, an exception is thrown.
In the next lesson, you will learn about the PreparedStatement interface.

ResultSet MetaData-SQL Warnings - Quiz

Click the Quiz link below to test your knowledge of ResultSetMetaData and SQLWarnings.
ResultSet MetaData-SQL Warnings - Quiz