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?
SQLWarningobjects are a subclass ofSQLExceptionthat 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
Connectionobject, aStatementobject (includingPreparedStatementandCallableStatementobjects), or aResultSetobject.Each of these classes has a
getWarningsmethod, which you must invoke in order to see the first warning reported on the calling object: