Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to call getWarnings() on Connections, Statements, and ResultSets with JDBC?

In JDBC the Connection, Statement, and ResultSet types each have a getWarnings() method that is specified to produce the first warning associated with objects of that type. The second and subsequent warnings, if they exist, are chained onto the first warning (if it even exists, null is produced if there are no warnings).

The specs say that warnings associated with objects of these types are cleared after certain actions. For example warnings on a ResultSet are cleared when each new row is read.

The SQLWarning type is a subtype of SQLException. So would the presence of a warning be indicated by an exception? And that exception would be chained to the associated object if the exception's runtime type is SQLWarning?

What I'm wondering is this, and it might be driver specific, how do I know when I should call getWarnings() and expect a non-null response? Put another way, is a warning present on a JDBC object and available with getWarnings() only after that object has thrown an exception? (and that exception is the warning?)

Should I call getWarnings() to look for warnings after every JDBC operation "just to be sure" if my goal is to observe every warning?

like image 236
Greg Mattes Avatar asked Jun 22 '11 02:06

Greg Mattes


People also ask

What does connection createStatement () do?

createStatement. Creates a Statement object for sending SQL statements to the database. SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.

What is connection and statement in JDBC?

JDBC makes it possible to do establish a connection with a data source, send queries and update statements, and process the results. Simply, JDBC makes it possible to do the following things within a Java application: Establish a connection with a data source. Send queries and update statements to the data source.

When we will get sql exception in Java?

Q #3) When SQLException can occur in Java? Answer: SQLException occurs if there is an error in the database access or other errors related to the database. When SQLException occurs, an object of type SQLException will be passed to the catch clause. We can handle it in the Catch block.


1 Answers

SQLWarning objects are a subclass of SQLException that deal with database access warnings.

Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned.

A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object.

Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object:

SQLWarning warning = stmt.getWarnings();
if (warning != null)
{
    System.out.println(\"n---Warning---n\");
    while (warning != null)
    {
        System.out.println(\"Message: \" + warning.getMessage());
        System.out.println(\"SQLState: \" + warning.getSQLState());
        System.out.print(\"Vendor error code: \");
        System.out.println(warning.getErrorCode());
        System.out.println(\"\");
        warning = warning.getNextWarning();
    }
}
like image 123
gmhk Avatar answered Nov 07 '22 16:11

gmhk