SQL Exception

  • Handling SQL Exceptions
    • The java.sql package provides the SQLException class, which is derived from the java.lang.Exception class.
    • You can catch the SQLException in a Java application using the try and catch exception handling block.
    • The SQLException class contains various methods that provide error information, these methods are:
      • int getErrorCode(): Returns the error code associated with the error occurred.
      • String getSQLState(): Returns X/Open error code.
      • SQLException getNextException(): Returns the next exception in the chain of exceptions.

Result Sets

ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.
A ResultSet object maintains a cursor that enables you to move through the rows stored in a ResultSet object.

Types of Result Sets

The sensitivity of the ResultSet object is determined by one of three different ResultSet types:

  • TYPE_FORWARD_ONLY:the result set is not scrollable i.e. the cursor moves only forward, from before the first row to after the last row.
  • TYPE_SCROLL_INSENSITIVE:the result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position.
  • TYPE_SCROLL_SENSITIVE:the result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. Before you can take advantage of these features, however, you need to create a scrollable ResultSet object

The following line of code illustrates one way to create a scrollable ResultSet object:
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("");
The first argument is one of three constants added to the ResultSet API to indicate the type of a ResultSet object: TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE.

The second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE.

If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY.