Reputation: 11132
I am trying to call a StoredProcedure from Java, but the result returned is always false. In reality, it has to return 100's of records. The connection is established good.
I have a stored procedure,
PROCEDURE get_records
(
grp1 IN a.name%TYPE DEFAULT NULL
,grp2 IN a.name%TYPE DEFAULT NULL
,grp3 IN a.name%TYPE DEFAULT NULL
,grp4 IN a.name%TYPE DEFAULT NULL
,grp5 IN a.name%TYPE DEFAULT NULL
,flag1 IN a.flag%TYPE DEFAULT 'F'
,flag2 IN a.flag%TYPE DEFAULT 'F'
,refercursor_out OUT SYS_REFCURSOR
);
My java Program:
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleTypes;
class DAO1 {
public static void main(String args[]) throws SQLException, IOException {
// Load the driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager
.getConnection("jdbc:oracle:thin:admin/admin@//host1:1521/abcdev");
System.out.println(conn);
// Prepare to call the stored procedure get_group_details.
CallableStatement cstmt = conn
.prepareCall("{call mypackage.get_records (?,?,?,?,?,?,?,?)}");
cstmt.registerOutParameter(8, OracleTypes.CURSOR); // REF CURSOR
cstmt.setString(1, "");
cstmt.setString(2, "");
cstmt.setString(3, "");
cstmt.setString(4, "");
cstmt.setString(5, "");
cstmt.setString(6, "");
cstmt.setString(7, "");
// execute get_records
//cstmt.execute(); //updated
cstmt.executeQuery(); //updated
ResultSet rs0 = (ResultSet) cstmt.getObject(8);
System.out.println("rs0 is " + rs0.next());
/*
* ResultSet rs = ((OracleCallableStatement) cstmt).getCursor(8);
* System.out.println("rs is " + rs.next());
*
* while (rs.next()) { System.out.println(".."); }
*/
// Close the statement
cstmt.close();
// Close the connection
conn.close();
}
}
Current ouput:
oracle.jdbc.driver.OracleConnection@5afec107
rs0 is false
Question - why is it false? I expect 100's of records in the resultset. There are records, and is visible on executing the test script from sql developer.
begin
mypackage.get_records(refercursor_out => :refercursor_out);
end;
// this returns 100's of records
Update: 08/08
The reason why I was not getting data is not the code issue, when I passed some value to the 6th parameter, it is returning data. Hence closing the thread; Marking @Klas Lindbäck's answer as the answer for suggesting the right execute method. [though execute(), executeQuery() and executeUpdate() in this case returns data in the resultset]
Upvotes: 0
Views: 3968
Reputation: 33273
You are not using the right semantics. execute
has other semantics than executeQuery
.
The execute method returns a boolean to indicate the form of the first result. You must call either the method getResultSet or getUpdateCount to retrieve the result; you must call getMoreResults to move to any subsequent result(s).
The easiest solution (smallest code change) for you is to switch to executeQuery
:
// execute get_records
cstmt.executeQuery();
Upvotes: 1