如何修复:java 中的 "Parameter index out of range (1 > number of parameters, which is 0)" 错误?

How to fix: "Parameter index out of range (1 > number of parameters, which is 0)" error in java?

我正在努力学习 JDBC 交易 我写了一些基本代码来测试它。但是,虽然我 运行 代码在输出中显示了一些错误。任何人都可以帮助我为什么会发生这种情况以及如何摆脱这种情况?

这是代码:

public class JDBCTransactionExample {

    private static String db_url = "jdbc:mysql://localhost:3306/test";
    private static String db_user = "root";
    private static String db_password = "root";

    private static final String INSERT_INTO_USER = "INSERT INTO user VALUES(?,?,?,?,?);";
    private static final String UPDATE_USER = "UPDATE user SET name = ? WHERE id = ?;";

    public static void main(String[] args) {

        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_password);
            conn.setAutoCommit(false);

            try {
                PreparedStatement insertStmt = conn.prepareStatement("INSERT_INTO_USER");
                PreparedStatement updateStmt = conn.prepareStatement("UPDATE_USER");

                // Create insert statement
                insertStmt.setInt(1, 4);
                insertStmt.setString(2, "BBB");
                insertStmt.setString(3, "bbb@gmail.com");
                insertStmt.setString(4, "en");
                insertStmt.setString(5, "7894");
                insertStmt.executeUpdate();

                // Create update statement
                updateStmt.setString(1, "AAA");
                updateStmt.setInt(2, 4);
                updateStmt.executeUpdate();

                // STEP 2 - Commit insert and update statement
                conn.commit();
                System.out.println("Transaction is commited successfully.");

            } catch (SQLException e) {
                e.printStackTrace();
                if (conn != null) {
                    try {
                        // STEP 3 - Roll back transaction
                        System.out.println("Transaction is being rolled back.");
                        conn.rollback();

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

这是输出:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).Transaction is being rolled back.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1372)
at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1385)
at com.mysql.cj.jdbc.ClientPreparedStatement.setInt(ClientPreparedStatement.java:1573)
at jdbc.transaction.JDBCTransactionExample.main(JDBCTransactionExample.java:29)

我想你是想在这里使用常量而不是 String:

PreparedStatement insertStmt = conn.prepareStatement("INSERT_INTO_USER");
PreparedStatement updateStmt = conn.prepareStatement("UPDATE_USER");

应该是

PreparedStatement insertStmt = conn.prepareStatement(INSERT_INTO_USER);
PreparedStatement updateStmt = conn.prepareStatement(UPDATE_USER);
private static final String INSERT_INTO_USER = "INSERT INTO user VALUES(?,?,?,?,?);";
private static final String UPDATE_USER = "UPDATE user SET name = ? WHERE id = ?;";

看了一下你的字符串,符号;是多余的,应该是这样的:

private static final String INSERT_INTO_USER = "INSERT INTO user VALUES(?,?,?,?,?)";
private static final String UPDATE_USER = "UPDATE user SET name = ? WHERE id = ?";
......
PreparedStatement insertStmt = conn.prepareStatement(INSERT_INTO_USER);
PreparedStatement updateStmt = conn.prepareStatement(UPDATE_USER);

基本上这个错误与 ?在preparedStatement中(?:参考preparedStatement的参数索引)

但是对于你的情况,你已经将 sql 查询声明为字符串,所以你不需要双引号,你只需要调用 CONST UPDATE_USER 和 UPDATE_USER !

使用: PreparedStatement insertStmt = conn.prepareStatement(INSERT_INTO_USER); PreparedStatement updateStmt = conn.prepareStatement(UPDATE_USER);