简体   繁体   English

Postgresql:使用Hibernate通过Java代码调用存储过程

[英]Postgresql :Calling of stored procedure through Java code with Hibernate

I am getting an error while using lambda expression(->{ ) for calling a stored procedure.使用 lambda 表达式(->{) 调用存储过程时出现错误。

Error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"错误:org.postgresql.util.PSQLException:错误:“{”处或附近的语法错误

Here is the code I am using to call my stored procedure using Prepared Statement.这是我用来使用 Prepared Statement 调用存储过程的代码。

    public void deleteData(Session session, int minVal, int maxVal) throws SQLException {
    session.doWork(connection -> {
        PreparedStatement st = connection.prepareStatement("{call delete_data(" + minVal + "," + maxVal + ")}");
        st.execute();
    });
} 

I want to execute this stored procedure.我想执行这个存储过程。 Could you please help me with it by suggesting changes or modifying my current code.您能否通过建议更改或修改我当前的代码来帮助我。

Thanks in advance.提前致谢。

Try This :尝试这个 :

public void deleteData(Session session, int minVal, int maxVal) throws SQLException {
    session.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    CallableStatement st = null;
                    st = connection.prepareCall("{call delete_data(?,?)}");
                    st.setInt(1, minVal);
                    st.setInt(2, maxVal);
                    st.execute();
                    st.close();
                }
            }); 

    }

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

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