用于将数据插入MySQL数据库的Java代码给出了一个“错误:没有为参数“% 2”指定值-如何修复?

5vf7fwbs  于 2023-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(128)

我正在为计算机科学做IB的内部评估,我有一段代码,基本上应该在主题表中插入一行,其中包含主题名称和该主题下的事件数量。
它能够通过引用数据库中另一个名为“事件”的单独表来查看事件的数量,并将事件的数量放入其中,其中将插入主题的名称。
然而,我得到的错误是:
错误:未为参数2指定值
这是怎么回事,我该如何解决?
下面是要使用的代码:

private void insertSubject_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    String command = evt.getActionCommand();

    String dbName = "ScheduleManagementDB";
    String tableName = "Subjects";
    String[] columnNames =
    {
        "Subject", "EventAmount"
    };
    String dbQuery = "INSERT INTO Subjects VALUES(?,?)";

    JavaMySQL_DB objDb = new JavaMySQL_DB(dbName);
    Connection myDbConn = objDb.getDbConn();
    PreparedStatement ps = null;
    try
    {
        ps = myDbConn.prepareStatement(dbQuery);
    }
    catch (SQLException ex)
    {
        System.out.print("error");
    }
    String Subject;
    int EventAmount;

    Subject = insertSubject_Field.getText();

    String getCountQuery = "SELECT COUNT(*) FROM Events WHERE SubjectE = ?";
    int count = 0;

    try
    {
        ps.setString(1, Subject);
        ps.executeUpdate();

        PreparedStatement getCountPs = myDbConn.prepareStatement(getCountQuery);
        getCountPs.setString(1, Subject);
        ResultSet rs = getCountPs.executeQuery();
        if (rs.next())
        {
            count = rs.getInt(1);
        }

        ps.setInt(2, count);
        ps.executeUpdate();

        System.out.println("Data Inserted");
    }
    catch (SQLException e)
    {
        System.out.println("Error: " + e.getMessage());
    }

我也试着不使用结果集,因为我认为getCountQuery语句无论如何都会工作,因为我只需要一个值,但这也不起作用。

ha5z0ras

ha5z0ras1#

在 * 绑定*两个参数之前,您无法调用PreparedStatementps。您当前绑定了第一个值,然后尝试调用execute update,然后尝试通过调用另一个PreparedStatement加载第二个值。先表演那个。此外,您当前泄漏了ConnectionStatement(s)和ResultSet(s)。要么自己关闭它们,要么使用try-with-Resources。就像

private void insertSubject_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String command = evt.getActionCommand();
    String dbName = "ScheduleManagementDB";
    String tableName = "Subjects";
    String[] columnNames = { "Subject", "EventAmount" };
    String dbQuery = "INSERT INTO Subjects VALUES(?,?)";
    String getCountQuery = "SELECT COUNT(*) FROM Events WHERE SubjectE = ?";

    JavaMySQL_DB objDb = new JavaMySQL_DB(dbName);

    String Subject = insertSubject_Field.getText();
    int count = 0;
    try (Connection myDbConn = objDb.getDbConn()) {
        try (PreparedStatement getCountPs = myDbConn
                    .prepareStatement(getCountQuery)) {
            getCountPs.setString(1, Subject);
            try (ResultSet rs = getCountPs.executeQuery()) {
                if (rs.next()) {
                    count = rs.getInt(1);
                }
            }
        }
        try (PreparedStatement ps = myDbConn.prepareStatement(dbQuery)) {
            ps.setString(1, Subject);
            ps.setInt(2, count);
            ps.executeUpdate();
            System.out.println("Data Inserted");
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

相关问题