【问题标题】:SQL update statement in eclipse will update database but will throws SQL exception [duplicate]eclipse中的SQL更新语句将更新数据库但会抛出SQL异常[重复]
【发布时间】:2017-04-17 20:10:56
【问题描述】:

我正在尝试从 Eclipse 更新数据库。数据库会更新,但是java程序会在数据库更新后抛出SQLException。

Statement stmnt = null;
    Connection connection = establishConnection();
    stmnt = connection.createStatement();
    stmnt.executeQuery("UPDATE table1  SET column2='"+description+"' WHERE column1='"+id+"'");

这是在控制台中打印的内容:

org.postgresql.util.PSQLException: No results were returned by the query

【问题讨论】:

标签: java eclipse postgresql jdbc


【解决方案1】:

executeQuery 应该用于返回 ResultSet 的语句。对于其他语句,您应该使用executeUpdate

stmnt.executeUpdate
    ("UPDATE table1  SET column2='"+description+"' WHERE column1='"+id+"'");

旁注:
以这种方式使用字符串操作可能会使您的代码受到 SQL 注入攻击。您应该考虑改用PreparedStatement

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2014-11-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多