How to use out parameter in oracleprepared statment - java

I have a procedure which has two output parameters and one object as an input parameter. I have to provide the input parameter as an ARRAY(SQL Array) but I have no idea how deal with the output parameters.
OraclePreparedStatement pstmt =
(OraclePreparedStatement)conn.prepareStatement(
"call stg_core.insert_flexi_gl_response(?,?,?)");
pstmt.registerReturnParameter(1, OracleTypes.VARCHAR);
pstmt.setARRAY( 3, array_to_pass );
I am getting a compilation error at OracleTypes.VARCHAR. I have tried importing all the option provided by eclipse but nothing is working.
I am getting the error OracleTypes cannot be resolved to a variable.
Kindly provide me any solution or workaround.

Related

SimpleJdbcCall: Unable to locate the corresponding parameter value for output parameter

I'm working on a project with a using sql server. I have a store procedure with one input (date) and one output (an int >0 if that date is a weekday). I'm getting this warning.
Unable to locate the corresponding parameter value for 'ISWEEKDAY' within the parameter values provided: [CurrentDate]
If I add the output parameter to the Map it gets rid of the warning, but this feels like it could get confusing when it comes to larger queries. Is there another way to prevent this warning?
MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("CurrentDate", dt);
//in.addValue("ISWEEKDAY", null); // Adding this line suppresses the warning

Java - Hibernate calling a MySql Stored Procedure - Warning Message

I have a Java project where I call a stored procedure using hibernate.
Here is sample code I am using
public String findCloudName(Long cloudId) {
LOG.info("Entered findCloudName Method - cloudId:{}", cloudId);
String cloudName = null;
ProcedureCall procedureCall = currentSession().createStoredProcedureCall("p_getCloudDetails");
procedureCall.registerParameter( "cloudId", Long.class, ParameterMode.IN ).bindValue( cloudId );
procedureCall.registerParameter( "cloudName", String.class, ParameterMode.OUT );
ProcedureOutputs outputs = procedureCall.getOutputs();
cloudName = (String) outputs.getOutputParameterValue( "cloudName" );
LOG.info("Exiting findCloudName Method - cloudName:{}", cloudName);
return cloudName;
}
This works fine and results the expected results.
However It leaves the following message in my logs
[WARN] [320]org.hibernate.procedure.internal.ProcedureCallImpl[prepareForNamedParameters] - HHH000456: Named parameters are used for a callable statement, but database metadata indicates named parameters are not supported.
I was looking through websites and the source code of hibernate to try and figure out how to get rid of this warning
Any help is greatly appreciated
Cheers
Damien
It's related to the correction of HHH-8740 :
In some cases, the database metadata is incorrect (i.e., says something is not supported, when it actually is supported). In this case it would be preferable to simply log a warning. If it turns out that named parameters really are not supported, then a SQLException will be thrown later when the named parameter is bound to the SQL statement.
So, the warning warn you from calling a method that queries metadata on named parameters.

Exception in thread "main" java.sql.SQLException: invalid name pattern: pakagename.sqlTypename

I am getting invalid name pattern exception when executing oracle function. which is return sql type as record which is present under package. If the type is present in Types directory it is working fine. But I am not able to execute if it is present in package directory. Please help me regarding this.
create or replace PACKAGE pkg_name
TYPE sqlTypeName
IS
RECORD(
firstVariable NUMBER,
secondVariable VARCHAR);
TYPE sqlTypeName_c is TABLE Of sqlTypeName INDEX BY pls_integer;
FUNCTION functionName() RETURN sqlTypeName_c
In java code:
Map typeMap = conn.getTypeMap();
typeMap.put("sqlTypeName_c", Array.class);
typeMap.put("sqlTypeName", Struct.class);
CallableStatement clstmt= null;
clstmt = conn.prepareCall("{ ? = call pkg_name.functionName() }");
clstmt.registerOutParameter(1, Types.ARRAY, "pkg_name.sqlTypeName_c");
clstmt.executeUpdate();
Array returnvalue = clstmt.getArray(1);
This will not work since the type is declared in the package. You will need to declare the sqlTypeName TYPE inside the schema.
Here are some of the questions which touch upon this issue -
Java- PLSQL- Call Table of records from java
Fetch Oracle table type from stored procedure using JDBC

Java callableStatement giving error : Attempt to set a parameter name that does not occur in the SQL

I want to execute a Oracle Stored procedure using named parameter from Java CollableStatement. Syntactically all is good by when we execute the application we get SQL Error-
Java Code -
int method1(){
CallableStatement stmt stmt = connection.prepareCall("{call "+strSQL.toString()+"}");
sp_copy_my_tree(?,?,?)
stmt.setInt("src_cd_ekey", 2057);
stmt.setInt("trg_ef_ekey", 8222);
stmt.setInt("trg_display_order", 1]);
returnValue = stmt.executeUpdate(strSQL.toString());
return returnValue ;
}
Oracle Stored Procedure -
create or replace PROCEDURE sp_copy_my_tree (src_ab_ekey IN NUMBER DEFAULT NULL,
src_cd_ekey IN NUMBER DEFAULT NULL, trg_ef_ekey IN NUMBER DEFAULT NULL,
trg_gh_ekey IN NUMBER DEFAULT NULL, trg_display_order IN NUMBER)
IS
begin
--- Some PL/SQL code ---
END ;
When I execute the above java statement I am getting Exception -
*java.sql.SQLException: Attempt to set a parameter name that does not occur in the SQL: src_cd_ekey*
Note- I have also tried to pass all the parameters in the same order of procedure, where for other 2 parameters I have passed null. But still getting the same Exception.
Please somebody help us to resolve this issue.
This issue came because of two reasons.
1) The Number of parameters declared in Procedure is different than the number of parameter passed from Java procedure call.
This is the reason why the aptly given Answer 1, to this thread cannot be used as is. We need to pass all 5 parameters from the procedure call in the proper sequence.
2) Named Parameter in Java is different then Oracle Named Parameter.
In Oracle we can execute procedure by passing only selected parameters, as the parameters passed may not be following the proper sequence so the values can be paired with the parameter name(as key). example -
EXEC sp_copy_my_tree (src_cd_ekey=>2057, trg_ef_ekey=>8222, trg_display_order=>1);
In Java we cannot emulate this and java named parameter has different meaning. In java statement we can specify the parameter name starting with colon instead of using ? as placeholder. Later while setting the placeholder we can use these parameter names rather then index. Example -
CallableStatement stmt stmt = connection.prepareCall( "{call sp_copy_my_tree(:src_cd_ekey,:trg_ef_ekey, :trg_display_order)}");
stmt.setInt("src_cd_ekey", 2057);
stmt.setInt("trg_ef_ekey", 8222);
stmt.setInt("trg_display_order", 1]);
But this call by Oracle will be considered as execution request to procedure with first 3 parameters.
Note: I was getting SqlException with stmt.executeUpdate(), so I used stmt.execute(). It might be a problem in my implementation but the latter worked in my case.
Try this method:
CallableStatement stmt = connection.prepareCall("{call sp_copy_my_tree (?,?,?}");
stmt.setInt(1, 2057);
stmt.setInt(2, 8222);
stmt.setInt(3, 1);
returnValue = stmt.executeUpdate();
return returnValue ;
}
See documentation for details: http://docs.oracle.com/cd/E11882_01/java.112/e16548/getsta.htm#i1008346

Calling an Oracle PL/SQL procedure with Custom Object return types from 0jdbc6 JDBCthin drivers

I'm writing some JDBC code which calls a Oracle 11g PL/SQL procdedure which has a Custom Object return type. Whenever I try an register my return types, I get either ORA-03115 or PLS-00306 as an error when the statement is executed depending on the type I set. An example is below:
PLSQL Code:
Procedure GetDataSummary (p_my_key IN KEYS.MY_KEY%TYPE,
p_recordset OUT data_summary_tab,
p_status OUT VARCHAR2);
More PLSQL Code (Custom Object Details):
CREATE OR REPLACE TYPE data_summary_obj
AS
OBJECT (data_key NUMBER,
data_category VARCHAR2 (100),
sensitive_flag VARCHAR2 (1),
date_created DATE,
date_rep_received DATE,
date_first_offering DATE,
agency_data_ref VARCHAR2 (13),
change_code VARCHAR2 (120),
data_ref VARCHAR2 (50),
data_status VARCHAR2 (100),
data_count NUMBER)
/
CREATE OR REPLACE TYPE data_summary_tab AS TABLE OF data_summary_obj
/
Java Code:
String query = "begin manageroleviewdata.getdatasummary(?, ?, ?); end;");
CallableStatement stmt = conn.prepareCall(query);
stmt.setInt(1, 83);
stmt.registerOutParameter(2, OracleTypes.CURSOR); // Causes error: PLS-00306
stmt.registerOutParameter(3, OracleTypes.VARCHAR);
stmt.execute(stmt); // Error mentioned above thrown here.
Can anyone provide me with an example showing how I can do this? I guess it's possible. However I can't see a rowset OracleType. CURSOR, REF, DATALINK, and more fail.
Apologies if this is a dumb question. I'm not a PL/SQL expert and may have used the wrong terminology in some areas of my question. (If so, please edit me).
Thanks in advance.
Regs, Andrew
I finally (with a little help from others) found out the answer to this. It came in three parts:
The first was that I needed to use an:
OracleCallableStatement stmt = (OracleCallableStatement) conn.prepareCall(query);
rather than the simple JDBC CallableStatement I had been trying to use.
The second part was that I had to register my "out" parameter as follows:
stmt.registerOutParameter(2, OracleTypes.STRUCT, "DATA_SUMMARY_TAB");
The third part, and it is implicit in part 2 above, was that "DATA_SUMMARY_TAB" had to be in UPPER CASE. If you put it in lower case, then you get a cryptic error message as follows:
java.sql.SQLException: invalid name pattern: MYTEST.data_summary_tab
at oracle.jdbc.oracore.OracleTypeADT.initMetadata(OracleTypeADT.java:553)
at oracle.jdbc.oracore.OracleTypeADT.init(OracleTypeADT.java:469)
at oracle.sql.StructDescriptor.initPickler(StructDescriptor.java:390)
at oracle.sql.StructDescriptor.(StructDescriptor.java:320)
That's it.
Also, please note our custom object type was not in any packages. If it is, you might need to hack the third parameter around a little.
You two different and perhaps contradictory error messages there.
PLS-00306: wrong number or types of arguments in call to 'string'
What is the dexcription of user defined type data_summary_tab? OracleTypes.CURSOR expects a REF CURSOR, which is equivalent to a JDBC ResultSet. Whereas data_summary_tab sounds like it might be a varray or nested table.
ORA-03115: unsupported network datatype or representation
This suggests you are using an older version of the client than the database server (say 10g or even 9i). Normally we can get away with it, but sometime it can cause bugs where we're doing uncommon things. I'm not sure whether calling user-defined types over JDBC ought to count as an "uncommon thing" but I suspect it may.

Categories