📄 e242. determining if a sql warning occurred.txt
字号:
Some database operations can cause a warning which is not handled by an exception. These warning must be explicitly checked for. An example of a warning is a data truncation error during a read operation (see the DataTruncation class).
There are three places to check for a warning --- on a Connection object, a Statement object, and a ResultSet object. This example demonstrates how to check for warning on each of these objects.
try {
// Get warnings on Connection object
SQLWarning warning = connection.getWarnings();
while (warning != null) {
// Process connection warning
// For information on these values, see e241 Handling a SQL Exception
String message = warning.getMessage();
String sqlState = warning.getSQLState();
int errorCode = warning.getErrorCode();
warning = warning.getNextWarning();
}
// Create a statement
Statement stmt = connection.createStatement();
// Use the statement...
// Get warnings on Statement object
warning = stmt.getWarnings();
if (warning != null) {
// Process statement warnings...
}
// Get a result set
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
while (resultSet.next()) {
// Use result set
// Get warnings on the current row of the ResultSet object
warning = resultSet.getWarnings();
if (warning != null) {
// Process result set warnings...
}
}
} catch (SQLException e) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -