简体   繁体   中英

PreparedStatements should throw Exception instead of warning

I am using PreparedStatements to write to database and sometimes I see following in my logs:

 2015-02-02 15:44:14,601  WARN SQL Warning Code: 1292, SQLState: 22007
 2015-02-02 15:44:14,602  WARN Incorrect datetime value: '2011' for column 'time_end' at row 1

However this is completely useless as I cannot see where in the code the problem is. Is it possible to force Connection/PreparedStatements to throw exceptions instead of these warnings?

This isn't much help, but you can retrieve the warnings by a call to Connection.getWarnings() or PreparedStatement.getWarnings() or ResultSet.getWarnings() and examine the warning object there. These methods all return an SQLWarning object, which is the first in a chain; call warning.getNextWarning() to see subsequent messages.

This, of course, means you'll have to alter all your DB code to track down the problems; sorry I don't have a better option right now. If there is a way to force the JDBC code to throw these as real catchable exceptions, it is probably driver-specific.

You can add a breakpoint at the line: "handler.prepare( warning )" in SqlExceptionHelper class, and then at the debugger (At least Intelli Idea does) have a stacktrace on the left side (on tab Frames), which you can navigate until you reach the class that executed the query, and work from there.

You also have all debugging variables, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM