查询结果集是否支持更新操作
try {
DatabaseMetaData dmd = connection.getMetaData();
if (dmd.supportsResultSetConcurrency(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
// Updatable result sets are supported
} else {
// Updatable result sets are not supported
}
} catch (SQLException e) {
}
创建一个可以更新的结果集
try {
// Create a statement that will return updatable result sets
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Primary key col_string must be specified so that the result set is updatable
ResultSet resultSet = stmt.executeQuery("SELECT col_string FROM my_table");
} catch (SQLException e) {
}
查询结果是否更新
try {
// Get concurrency of the result set
int concurrency = resultSet.getConcurrency();
if (concurrency == ResultSet.CONCUR_UPDATABLE) {
// Result set is updatable
} else {
// Result set is not updatable
}
} catch (SQLException e) {
}
利用结果集更新行数据
try {
// Create an updatable result set
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Move cursor to the row to update
resultSet.first();
// Update the value of column col_string on that row
resultSet.updateString("col_string", "new data");
// Update the row; if auto-commit is enabled, update is committed
resultSet.updateRow();
} catch (SQLException e) {
}
取消结果集的更新操作
try {
// Create an updatable result set
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Move cursor to the row to update
resultSet.first();
// Update the value of column col_string on that row
resultSet.updateString("col_string", "new data");
// Discard the update to the row
resultSet.cancelRowUpdates();
} catch (SQLException e) {
}
利用结果集删除数据行
try {
// Create an updatable result set
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Delete the first row
resultSet.first();
resultSet.deleteRow();
} catch (SQLException e) {
}
刷新可更新的结果集
try {
// Create an updatable result set
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Use the result set...
// Retrieve the current values of the row from the database
resultSet.refreshRow();
} catch(SQLException e) {
}
插入数据利用可更新的数据结果集
try {
// Create an updatable result set
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
// Move cursor to the "insert row"
resultSet.moveToInsertRow();
// Set values for the new row.
resultSet.updateString("col_string", "new data");
// Insert the row
resultSet.insertRow();
} catch (SQLException e) {
}