JDBC 中的 CONCUR_READ_ONLY ResultSet 是什么?解释一下?
jdbcjava 8programmingobject oriented programming更新于 2025/1/29 12:28:17
通常,您会将其作为并发类型的 ResultSet 值传递给 createStatement() 方法。
Statement createStatement(int resultSetType, int resultSetConcurrency)
这种类型的结果集不可更新。也就是说,一旦获取 ResultSet 对象,就无法更新其内容。
示例
假设数据库中有一个名为 Employee 的表,其内容如下:
+----+---------+--------+----------------+ | Id | Name | Salary | Location | +----+---------+--------+----------------+ | 1 | Amit | 3000 | Hyderabad | | 2 | Kalyan | 4000 | Vishakhapatnam | | 3 | Renuka | 6000 | Delhi | | 4 | Archana | 96000 | Mumbai | | 5 | Sumith | 11000 | Hyderabad | | 6 | Rama | 11000 | Goa | | 7 | Mahesh | 5300 | Vishakhapatnam | | 8 | Ramesh | 12000 | Hyderabad | | 9 | Suresh | 7600 | Pune | | 10 | Santosh | 96000 | Mumbai | +----+---------+--------+----------------+
在下面的示例中,我们尝试将员工的工资值增加 5000 并打印结果。
import java.sql.*;
public class Updatable {
public static void main(String[] args) throws Exception {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获取连接
String mysqlUrl = "jdbc:mysql://localhost/TestDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("连接已建立......");
//创建 Statement 对象
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//检索数据
ResultSet rs = stmt.executeQuery("select * from Employees");
//打印表内容
System.out.println("Contents of the table: ");
rs.beforeFirst();
while(rs.next()) {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Salary: " + rs.getInt("Salary"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.println(", Location: " + rs.getString("Location"));
}
System.out.println();
//将指针移动到 ResultSet 中的起始位置
rs.beforeFirst();
//将每位员工的工资更新 5000
while(rs.next()) {
//Retrieve by column name
int newSal = rs.getInt("Salary") + 5000;
rs.updateInt( "Salary", newSal );
rs.updateRow();
}
System.out.println("Conetnets of the resultset after increaing salaries");
rs.beforeFirst();
while(rs.next()) {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Salary: " + rs.getInt("Salary"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.println(", Location: " + rs.getString("Location"));
}
System.out.println();
}
}
Error
由于我们创建的 ResultSet 是只读的,因此您无法更新 ResultSet 的内容,并且会收到以下错误。
连接已建立...... Contents of the table: ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam ID: 3, Salary: 11000, Name: Renuka, Location: Delhi ID: 4, Salary: 101000, Name: Archana, Location: Mumbai ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad ID: 6, Salary: 16000, Name: Rama, Location: Goa ID: 7, Salary: 10300, Name: Mahesh, Location: Vishakhapatnam ID: 8, Salary: 17000, Name: Ramesh, Location: Hyderabad ID: 9, Salary: 12600, Name: Suresh, Location: Pune ID: 10, Salary: 101000, Name: Santosh, Location: Mumbai Exception in thread "main" com.mysql.jdbc.NotUpdatable: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details. at com.mysql.jdbc.ResultSetImpl.updateInt(ResultSetImpl.java:8457) at com.mysql.jdbc.ResultSetImpl.updateInt(ResultSetImpl.java:8475) at NewSet.Updatable.main(Updatable.java:42)
相关文章
打印
下一节:JDBC 中的已连接行集 (Row Set) 和已断开行集 (Drow Set) 是什么? ❯❮ 上一节:JDBC 中的 CONCUR_UPDATABLE ResultSet 是什么?解释一下?

