简体   繁体   English

使用问题? 从 Java 更新时 MySQL 存储过程的通配符

[英]Problems using the ? wildcard for a MySQL stored procedure when updating from Java

I have the following Java class that accepts 11 parameters and uses them to create a new record in a database.我有以下 Java 类,它接受 11 个参数并使用它们在数据库中创建新记录。 The stored procedure is tested and works in MySQL, but I can't get the Java class accessing it to work:存储过程已经过测试并且可以在 MySQL 中运行,但是我无法让访问它的 Java 类正常工作:

public void insert_saved_pub_set(int user_id, String presProfile, String procProfile, String dateLabel, String doc_id, String pubName, String lastLoaded, String publicV, String label, String description, String out_id)
                throws SQLException {                   

    String sql = "call insert_saved_pub_set(?,?,?,?,?,?,?,?,?,?,?);";

    PreparedStatement stmt = conn.prepareStatement(sql);

    stmt.setInt(1, user_id);        
    stmt.setInt(2, Integer.parseInt(presProfile));
    stmt.setInt(3, Integer.parseInt(procProfile));
    stmt.setString(4, dateLabel);
    stmt.setInt(5, Integer.parseInt(doc_id));
    stmt.setString(6, pubName);
    stmt.setInt(7, Integer.parseInt(lastLoaded));
    stmt.setInt(8, Integer.parseInt(publicV));
    stmt.setString(9, label);
    stmt.setString(10, description);
    stmt.setInt(11, Integer.parseInt(out_id));

    stmt.executeUpdate(sql);

}

The error I get when I attempt to test this is:我尝试测试时遇到的错误是:

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?,?,?,?,?,?)' at line 1检查与您的 MySQL 服务器版本相对应的手册,在第 1 行的“?,?,?,?,?,?,?,?,?,?,?)”附近使用正确的语法

I've tested the prepared statement from Java with hardcoded values and it works, but I've doing something stupid with the wildcards and I've stared at it too long.我已经使用硬编码值测试了 Java 中准备好的语句并且它可以工作,但是我用通配符做了一些愚蠢的事情并且我盯着它看得太久了。

** comment added post-fix below ** ** 在下方添加了修复后评论 **

Just to close this off, the fixed code looks like this:只是为了关闭它,固定代码如下所示:

public void insert_saved_pub_set(int user_id, String presProfile, String procProfile, String dateLabel, String doc_id, String pubName, String lastLoaded, String publicV, String label, String description, String out_id)
            throws SQLException {               

        String sql = "call insert_saved_pub_set(?,?,?,?,?,?,?,?,?,?,?);";       

        CallableStatement cStmt = conn.prepareCall(sql);        

        cStmt.setInt(1, user_id);       
        cStmt.setInt(2, Integer.parseInt(presProfile));
        cStmt.setInt(3, Integer.parseInt(procProfile));
        cStmt.setString(4, dateLabel);
        cStmt.setInt(5, Integer.parseInt(doc_id));
        cStmt.setString(6, pubName);
        cStmt.setInt(7, Integer.parseInt(lastLoaded));
        cStmt.setInt(8, Integer.parseInt(publicV));
        cStmt.setString(9, label);
        cStmt.setString(10, description);   
        cStmt.setInt(11, Integer.parseInt(out_id)); 

        cStmt.executeUpdate();
        }       

    }

The executeUpdate(String) method is inherited from Statement and attempts to execute the given SQL statement as is -- without placeholder substitution. executeUpdate(String)方法继承自Statement并尝试按原样执行给定的 SQL 语句——没有占位符替换。

The executeUpdate() method (no parameters), declared in PreparedStatement , will perform placeholder substitution.所述executeUpdate()方法(无参数),在声明PreparedStatement ,将执行占位符替换。 This works with PreparedStatements such as update statements.这适用于PreparedStatements例如更新语句。

In addition, you aren't running a straight SQL statement here.此外,您没有在此处运行直接的 SQL 语句。 You are calling a stored procedure.您正在调用存储过程。 Switch from PreparedStatement to CallableStatement .PreparedStatement切换到CallableStatement

CallableStatement cStmt = conn.prepareCall(sql);

The "set" methods work as before, but then you call execute() to execute the CallableStatement . “set”方法像以前一样工作,但随后您调用execute()来执行CallableStatement

To call a stored procedure, you need a CallableStatement , not a PreparedStatement .要调用存储过程,您需要CallableStatement ,而不是PreparedStatement Quoting CallableStatement Javadoc:引用CallableStatement Javadoc:

The interface used to execute SQL stored procedures.用于执行 SQL 存储过程的接口。

This is a sample code to get you started:这是帮助您入门的示例代码:

CallableStatement cStmt = conn.prepareCall("{call insert_saved_pub_set(?,?,?,?,?,?,?,?,?,?,?)}");
cStmt.setInt(1, user_id);
// ... rest of code

boolean hadResults = cStmt.execute(); // executes the stored procedure
while (hadResults) {
    ResultSet rs = cStmt.getResultSet();

    // process result set
    ...

    hadResults = cStmt.getMoreResults();
}

I suggest you read MySQL documentation about the usage of stored procedure in Java code.我建议您阅读有关在 Java 代码中使用存储过程的MySQL 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM