Reputation: 2843
I am trying to invoke stored procedure of Oracle using Oracle JDBC.
I am very new to Oracle and hence having difficulty figuring out how to invoke the SP and get the proper output.
The Stored procedure below is supposed to give me just 4 outputs with no input.
Code snippet is :
cStmt=connection.prepareCall(" {call userName.user(?)}");
cStmt.registerOutParameter(1, OracleTypes.CURSOR);
cStmt.executeUpdate();
rst = (ResultSet) cStmt.getObject(1);
while (rst.next()) {
int id = rst.getInt(1);
int Id1=rst.getInt(2);
String accoutNum=rst.getString(3);
String accountName=rst.getString(4);
System.out.println("valeus");
System.out.println(id);
System.out.println(Id1);
System.out.println(accoutNum);
System.out.println(accountName);
}
Now running this code is giving me following error:
java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'user'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:505)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:223)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:204)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1041)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1328)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3593)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3674)
at oracle.jdbc.driver.OracleCallableStatement.executeUpdate(OracleCallableStatement.java:4780)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
at demo.Oracle.main(Oracle.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
I am sure there's a definite problem with the syntax. I googled a lot but I am not able to find the concrete solution..
Can someone please guide me.. JDBC jar version is 6.0
the Header of procedure is :
create or replace
procedure user is
cursor user_cursor is select * from user_master;
v_rec_user user_cursor%rowtype;
Upvotes: 0
Views: 1576
Reputation: 191275
Your procedure definition has no parameter; the way you're calling it you're expecting it to have an OUT parameter of type sys_refcursor
, or a schema-level cursor type.
I think you want something like this:
create or replace procedure user(result out sys_refcursor) is
begin
open result for
select * from user_master;
end;
/
But as you've only shown the start it isn't clear what you're using v_rec_cursor
for.
(Also, user
is not a good name for the procedure as it's a reserved word; maybe something like get_users
would be better?)
Small Java program to call that procedure and print the results; this compiles and runs for me (with *
replaced with real DB connection details):
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;
import oracle.jdbc.pool.OracleDataSource;
public final class DBTest
{
public static void main(String[] args)
throws SQLException
{
OracleDataSource datasource;
Connection conn = null;
CallableStatement cStmt = null;
ResultSet rSet = null;
try
{
datasource = new OracleDataSource();
String jdbcURL = "jdbc:oracle:thin:*/*@*:1521:*";
datasource.setURL(jdbcURL);
conn = datasource.getConnection();
cStmt = conn.prepareCall("{ call get_user(?) }");
cStmt.registerOutParameter(1, OracleTypes.CURSOR);
cStmt.execute();
rSet = (ResultSet) cStmt.getObject(1);
while (rSet.next())
{
System.out.println(rSet.getInt(1)
+ ":" + rSet.getInt(2)
+ ":" + rSet.getString(3)
+ ":" + rSet.getString(4));
}
}
finally
{
if (rSet != null)
{
rSet.close();
}
if (cStmt != null)
{
cStmt.close();
}
if (conn != null)
{
conn.close();
}
}
}
}
Upvotes: 2