【问题标题】:How to update Tables with an input from the user [duplicate]如何使用用户的输入更新表格 [重复]
【发布时间】:2016-07-17 23:20:31
【问题描述】:

所以事情就是这样,我正在尝试更新我的表,更新用户输入 student 值的 exam 标记。

所以我正在使用这个查询。

updateExamMark = connection.prepareStatement("UPDATE Results SET exam= ? WHERE student = ?");

这是我的方法(不完整的代码)

public List< Results > updateExamMark( int exam, String student )
 {
  List< Results > results = null;
  ResultSet resultSet = null;

  try 
  {
      updateExamMark.setInt(1, exam ); 
      updateExamMark.setString(2, student);


     // executeQuery returns ResultSet containing matching entries
     resultSet = updateExamMark.executeQuery(); 

     results = new ArrayList< Results >();

我收到此错误“无法使用执行查询方法进行更新”

我的查询有什么问题?

【问题讨论】:

标签: java sql jdbc


【解决方案1】:

执行查询方法不能用于更新

resultSet = updateExamMark.executeQuery(); 

改成

int r = updateExamMark.executeUpdate(); 

您应该使用executeUpdate() 来执行非选择查询。

【讨论】:

    【解决方案2】:

    您需要将executeQuery() 更改为executeUpdate(),因为第一种方法用于执行SELECT 查询。当您需要更新(INSERTDELETEUPDATE...)时,您必须使用第二个。

    所以声明

    resultSet = updateExamMark.executeQuery(); 
    

    会将SELECT 语句的所有结果返回到resultSet。将此语句更改为

    int rowsAffected = updateExamMark.executeUpdate(); 
    

    这将返回受更新影响的行数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多