sqlexception

gorkyyrv  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(253)

我有一段代码,它应该执行一个存储过程并返回结果。

public static boolean finishOrderInteraction(int orderId, int departmentId){
        try{
            boolean result = true;
            Connection con = DriverManager.getConnection(SERVER, USER, PASSWORD);
            PreparedStatement stmt = con.prepareStatement("CALL finishOrderInteraction(?, ?, @result); SELECT @result AS Result;"); //returns true, if an SQLExeption accured during the procedure execution
            stmt.setInt(1, orderId);
            stmt.setInt(2, departmentId);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()){
                result = rs.getBoolean("Result");
            }
            con.close();
            return !result;
        }catch (SQLException e){
            Logger.Severe("Failed to update order status", e, "-");
            return false;
        }
    }

我的问题是,此代码产生以下异常:

SQLException: (conn=122) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT @result AS Result' at line 1

但是当我通过mysql工作台尝试相同的查询时,一切都如预期的那样工作。
mysql工作台查询:

CALL finishOrderInteraction(126, 1, @result); SELECT @result AS Result;

我就是搞不清楚我做错了什么。为什么查询在mysql工作台中工作而在我的方法中不工作?

kcrjzv8t

kcrjzv8t1#

多亏了蒂姆·比盖莱森的回答,我才弄明白。结果发现,这个异常有点误导,因为即使有语法错误,这个语法错误也不是我没有得到想要的结果的原因。
真正的问题是我 con.prepareStatement(...) 而不是 con.prepareCall(...) ,因为 con.prepareCall(...) 创建 CallableStatement 可以注册一个out参数。由于这个参数,所以不需要进行第二次查询 SELECT @result AS Result; 在我的原始语句中,这导致了语法错误。
我最终得到了这样的工作代码:

public static boolean finishOrderInteraction(int orderId, int departmentId){
        try{
            Boolean result = true;
            Connection con = DriverManager.getConnection(SERVER, USER, PASSWORD);
            CallableStatement stmt = con.prepareCall("CALL finishOrderInteraction(?, ?, ?)");
            stmt.setInt(1, orderId);
            stmt.setInt(2, departmentId);
            stmt.registerOutParameter(3, Types.BOOLEAN);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()){
                result = rs.getBoolean(1);
            }
            con.close();
            return result;
        }catch (SQLException e){
            Logger.Severe("Failed to finish order interaction.", e, "-");
            return false;
        }
    }
cczfrluj

cczfrluj2#

你需要注册你的 OUT 参数,然后在对语句调用update后访问值:

public static boolean finishOrderInteraction(int orderId, int departmentId) {
    Boolean result = true;

    try {
        Connection con = DriverManager.getConnection(SERVER, USER, PASSWORD);
        CallableStatement stmt = con.prepareCall("CALL finishOrderInteraction(?, ?, ?)");
        stmt.setInt(1, orderId);
        stmt.setInt(2, departmentId);
        stmt.registerOutParameter(3, java.sql.Types.BOOLEAN);

        stmt.executeUpdate();

        result = stmt.getBoolean(1);
        con.close();
    }
    catch (SQLException e) {
        Logger.Severe("Failed to update order status", e, "-");
        return false;
    }

    return result;
}

相关问题