Reputation: 385
I don't understand what the difference between in Connection.getWarning() and Statement.getWarnings() ? Is the main difference that Connection.getWarning contains warnings for all statements and Statement .getWarnings contains warnings for single statement?
Upvotes: 0
Views: 605
Reputation: 109021
Connection.getWarnings
returns connection-level warnings, while Statement.getWarnings
returns statement-level warnings. There is also ResultSet.getWarnings
, which (surprise) returns result-set-level warnings. In other words, if a method called on a Connection
produces a warning, it is added on the connection, for a method called on a Statement
on the statement, and for a method of a ResultSet
on the result set.
In general, warnings produced by a Statement
should not be reported on the Connection
, and warnings on a ResultSet
should not be reported on the Statement
. See for example Statement.getWarnings()
which says:
Note: If you are processing a
ResultSet
object, any warnings associated with reads on thatResultSet
object will be chained on it rather than on theStatement
object that produced it.
and ResultSet.getWarnings()
which says:
Note: This warning chain only covers warnings caused by
ResultSet
methods. Any warning caused byStatement
methods (such as reading OUT parameters) will be chained on theStatement
object.
The JDBC specification is not very detailed on warnings, it only says (JDBC 4.3, section 8.2 SQLWarning):
When a method generates an
SQLWarning
object, the caller is not informed that a data access warning has occurred. The methodgetWarnings
must be called on the appropriate object to retrieve theSQLWarning
object. However, theDataTruncation
sub-class ofSQLWarning
may be thrown in some circumstances, see Section 8.3 “DataTruncation” on page 8-46 for more details.If multiple data access warnings occur, they are chained to the first one and can be retrieved by recursively calling the
SQLWarning.getNextWarning
method. If there are no more warnings in the chain,getNextWarning
returns null.Subsequent
SQLWarning
objects continue to be added to the chain until the next statement is executed or, in the case of aResultSet
object, when the cursor is repositioned, at which point allSQLWarning
objects in the chain are removed.
and in section 8.3 DataTruncation:
When data truncation occurs on a read from the data source, a
SQLWarning
is reported.
and in section 15.1.1 ResultSet
Types:
If the driver does not support the type supplied to the methods
createStatement
,prepareStatement
, orprepareCall
, it generates anSQLWarning
on theConnection
object that is creating the statement.
Similar text can be found in 15.1.2 ResultSet
Concurrency and 15.1.3.1 Determining ResultSet
Holdability.
Sometimes the JDBC API documentation explicitly mention warnings (eg Connection.setClientInfo(String, String)
and ResultSet.next()
).
Although not specified by JDBC, drivers will commonly also report warnings during connect on the Connection
.
Upvotes: 2
Reputation: 171
The Object type returned by the getWarnings method of Connection, Statement is not different. It is of type SQLWarning.
But depending on the context it has been called it differs.
Upvotes: 1