champ.exe
champ.exe

Reputation: 125

Postgresql :Calling of stored procedure through Java code with Hibernate

I am getting an error while using lambda expression(->{ ) for calling a stored procedure.

Error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"

Here is the code I am using to call my stored procedure using 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.

Upvotes: 1

Views: 198

Answers (1)

Rakesh Vasal
Rakesh Vasal

Reputation: 266

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();
                }
            }); 

    }

Upvotes: 1

Related Questions