【问题标题】:Failing to Update a result set from msql table [duplicate]无法从 sql 表更新结果集 [重复]
【发布时间】:2019-04-17 06:06:03
【问题描述】:

我在更新 mssql 给出的结果集时遇到问题。

我想用添加 (初始AmountDeposited)。 AmountDeposited 在名为 (textFieldamount) 的文本字段中作为双精度输入,初始也是结果集中的双精度值。

我收到一个错误。 (结果集不可更新(引用的表没有主键)。

此结果集必须来自使用 ResultSet.CONCUR_UPDATABLE 结果集类型创建的语句。)

这是我的代码

String url = "jdbc:mysql://localhost:3306/STATTER_BANK";
String user = "root";
String password = "";
double amountDeposited = Double.parseDouble(textFieldamount.getText());

public void theQuery {
    String query1 = "SELECT initial_deposit FROM createaccount"+"WHERE id=1";

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection(url, user, password);
        PreparedStatement stt = con.prepareStatement(query1, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

       ResultSet rs = stt.executeQuery();

       while (rs.next()) {
           double initial = rs.getDouble("initial_deposit");
           rs.updateDouble( "initial_deposit", initial + amountDeposited);
           rs.updateRow();
       }
    } catch (Exception e ) {
        e.printStackTrace();  
    }
}

【问题讨论】:

    标签: java sql-server tsql


    【解决方案1】:

    可以参考。
    similar question perhaps it is the same one
    使用 ResultSet.TYPE_SCROLL_INSENSITIVE 更改 ResultSet.TYPE_SCROLL_SENSITIVE 应该可以工作。

    【讨论】:

    • 您应该将其添加为评论,而不是答案。如果一个问题是重复的,你应该这样标记它。
    • @RealSkeptic 我没有那么多评论点(声誉)
    • 解决方案是写出正确的答案,并获得所需的分数。你离那不远了。不正确的答案是不可行的。
    • @RealSkeptic 是的。我最近开始了 SO 。
    • 我已经尝试了 ResultSet.TYPE_SCROLL_INSENSITIVE 仍然得到同样的错误
    猜你喜欢
    • 2022-01-19
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2023-03-21
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多