Question

I am trying to make a simple PL/SQL-to-Java setup, where I make a function in PL/SQL and call it from Java, expecting a return value.

I am fairly new to PL/SQL and calling it in Java so my approach is probably just lacking understanding in how return values from SQL actually works.

My SQL Function looks like this:

CREATE OR REPLACE FUNCTION GET_BOOKED_COUNT(P_NO VARCHAR2)
  RETURN NUMBER AS
  RESULTS NUMBER := 0;
BEGIN
  SELECT COUNT(*) INTO RESULTS FROM SEAT WHERE PLANE_NO=P_NO AND BOOKED IS NULL;
  RETURN RESULTS;
END GET_BOOKED_COUNT;

Return a number pretty much. It needs to check a table of seats to see how many of them are not booked yet.

After trying to follow various other Stackoverflow posts, I tried to duplicate what they did but it keeps telling me that the Index Column is Invalid.

public static boolean isAllBooked(String plane_no) {
    String sql = "{? = CALL GET_BOOKED_COUNT('?')}";
    boolean res = false;
    try {
        Connection conn = getConnection("db_010", "db2014");
        CallableStatement cs = conn.prepareCall(sql);
        cs.setString(2, plane_no);
        cs.registerOutParameter(1, Types.NUMERIC);
        cs.executeUpdate();
        int i = cs.getInt(1);
        if(i == 0) {
            res = true;
        }
        conn.close();
    } catch (SQLException ex) {
        Logger.getLogger(HelperClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    return res;
}

It's probably an easy fix, so what am I doing wrong exactly?

Was it helpful?

Solution

Your second bind variable, the argument to the function, should not be enclosed in single quotes. You're passing the literal value '?', not defining a bind:

String sql = "{? = CALL GET_BOOKED_COUNT(?)}";

The way you then refer to both parameters seems to be OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top