e242. determining if a sql warning occurred.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 41 行
TXT
41 行
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 + =
减小字号Ctrl + -
显示快捷键?