gpt4 book ai didi

java - Mysql Java Derby Netbeans : 'deleteRow' not allowed because the ResultSet is not an updatable ResultSet

转载 作者:行者123 更新时间:2023-11-29 02:21:47 25 4
gpt4 key购买 nike

我正在尝试删除一行但它不允许我,它说“'deleteRow' 不允许,因为 ResultSet 不是可更新的 ResultSet”。这是我的代码:

public void addUserName() throws SQLException {
rs = st.executeQuery("select NAME, prize from usernames");
try {
while (rs.next()) {
String attribute2 = rs.getString("name");

if (attribute2.equals(getName())){
String toPrint = rs.getString(2);
System.out.println("Welcome back " + getName());
System.out.println("You previously won " + toPrint);
System.out.println("Let's see if you can do any better this time :)");
rs.deleteRow();

}
}

} catch (SQLException ex) {
Logger.getLogger(Millionaire.class.getName()).log(Level.SEVERE, null, ex);
}

}

我在这里做错了什么?任何建议将不胜感激。提前致谢。

这就是我创建表格的方式,并添加了给定的建议,但仍然出现相同的错误:

public void createTable(String tableName) {
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//String newTable = "millionair";

//statement.executeUpdate("drop table if exists "+newTable);
String sqlCreateTable = "CREATE TABLE " + tableName + " (NAME VARCHAR(50), PRIZE INT)";

stmt.executeUpdate(sqlCreateTable);
} catch (SQLException ex) {

System.err.println("SQLException: " + ex.getMessage());
}
}

最佳答案

来自docs

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.

   Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable

因此您必须在创建语句时设置ResultSet.CONCUR_UPDATABLE 属性。

关于java - Mysql Java Derby Netbeans : 'deleteRow' not allowed because the ResultSet is not an updatable ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30477892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com