不允许参数标记(更新 Access 数据库)

parameter marker not allowed (updating Access database)

Statement st = connect.createStatement();
String sql = "update StudentDatabaseS set RollNo = ?, FullName = ?, FatherName = ?, FatherCNIC = ?, DateOfBirth = ?, Class = ?, Address = ?, City = ?, Province = ? where RollNo = '"+Srollno+"'";
ResultSet rs = st.executeQuery(sql);

ps = connect.prepareStatement(sql);

ps.setInt(1, roll_mo);
ps.setString(2, name_mo);
ps.setString(3, Fname_mo);
ps.setString(4, fcnic_mo);
ps.setString(5, dob_mo);
ps.setInt   (6, Class_mo);
ps.setString(7, add_mo);
ps.setString(8, city_mo);
ps.setString(9, prvnce_mo);
ps.executeUpdate();

我正在尝试更新 Access 数据库中的记录,但它不断向我抛出异常,例如不允许参数标记。

有人可以帮我解决这个问题吗?

何时

String sql = "update StudentDatabaseS set RollNo = ?, FullName = ?, FatherName = ?, FatherCNIC = ?, DateOfBirth = ?, Class = ?, Address = ?, City = ?, Province = ? where RollNo = '"+Srollno+"'";

ResultSet rs = st.executeQuery(sql);

没有意义,因为

  1. 该查询没有 return 结果集,并且
  2. executeQuery 无法知道哪些值对应于参数占位符 (?)。

如果省略该行,则 "parameter marker not allowed" 错误将消失,您可以使用 PreparedStatement 继续更新。

try{
         Statement st = connect.createStatement();
         String sql = "update StudentDatabaseS set RollNo = ?, FullName = ?, FatherName = ?, FatherCNIC = ?, DateOfBirth = ?, Class = ?, Address = ?, City = ?, Province = ? where RollNo = '"+Srollno+"'";
         try {
        ps = connect.prepareStatement(sql);

        ps.setInt(1, roll_mo);
        ps.setString(2, name_mo);
        ps.setString(3, Fname_mo);
        ps.setString(4, fcnic_mo);
        ps.setString(5, dob_mo);
        ps.setInt   (6, Class_mo);
        ps.setString(7, add_mo);
        ps.setString(8, city_mo);
        ps.setString(9, prvnce_mo);
        ps.executeUpdate();
        Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(null, "Record Modified !");
        search_input.requestFocus();
         }

         catch(Exception ex)
         {
               ex.printStackTrace();
         }

    }

         catch(Exception e)
    {
        Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(null, e);
    }