I am getting an Error while running this:
1. cs = getCon1().prepareCall("{CALL SaveLabourWageDetails(?,?)}");
2. cs.setString(1, user.getUserId());
3. cs.registerOutParameter(2, java.sql.Types.INTEGER); //<--- ERROR at this line
4. cs.execute();
5. String lastIsertId=cs.getString(2);
The Stored Procedure is :
CREATE
PROCEDURE `cheque_alert`.`SaveLabourDetailsHead`(IN wage_entered_by VARCHAR(10),OUT LastInsertId INT)
BEGIN
INSERT INTO `cheque_alert`.`labour_wage_head`
(
`wage_entered_by`,
`entered_date_time`)
VALUES (wage_entered_by,
NOW());
SELECT LAST_INSERT_ID() INTO LastInsertId;
END$$
DELIMITER ;
Please point out the problem in this code..
You are calling wrong procedure. You have procedure SaveLabourDetailsHead and you are calling
1. cs = getCon1().prepareCall("{CALL SaveLabourWageDetails(?,?)}");
↑
Change to,
1. cs = getCon1().prepareCall("{CALL SaveLabourDetailsHead(?)}");
Set String parameter wage_entered_by.
Your out parameter is of type String, but it should be int. Try this out.
int lastIsertId=cs.getInt(2);
I had the same problem but the output exception is misleading as the root cause was the procedure name.
I typed incorrect procedure which did not exist in database.
Instead of providing a SQL exception something like "routine does not exist", it gave:
java.sql.SQLException: Parameter number 2 is not an OUT parameter.
Just had the same problem.
It was a permission issue in my case. The MySQL user my application uses lacked the EXECUTE permission on the schema I'm working on.
GRANT EXECUTE ON <your_schema>.* TO '<your_user>'#'localhost';
FLUSH PRIVILEGES;
Related
I am trying to call a Procedure from a package from my java application but receive an error. The code is set up like so...
------Java-----
Connection = conn;
String call_code="{? = call MY_PROCEDURE.Process_vale(?,?,?)}";
CallableStatement Process_cs=conn.prepareCall(call_code);
String bValue= "12345";
Process_cs.setString(1, bValue);
Process_cs.registerOutParameter(2, Types.INTEGER);
Process_cs.registerOutParameter(3, Types.VARCHAR);
Process_cs.execute();
The Procedure is setup like:
PROCEDURE Process_value (bValue VARCHAR2, PN_CD OUT NUMBER, PN_MSG OUT VARCHAR2).....
The Error I see is:
java.sql.SQLException: Missing IN or OUT parameter at index:: 4
I am curious as to why it mentions an index of 4, when the Procedure only has 3 parameters, I believe I am missing a critical fact here.
You have done some serious mistakes here. First Since you are calling a procedure you wont get any return value, only functions can have return values. So you have to call the procedure as follows.
String call_code="{call MY_PROCEDURE.Process_vale(?,?,?)}";
You could be successfully execute this if your procedure first parameter is IN type and other two parameters are OUT type.
DELIMITER //
CREATE PROCEDURE temp ( empId INT)
BEGIN
DECLARE var_etype VARCHAR(36);
SELECT
emptype = QOUTE(emptype)
FROM
dms_document
WHERE
id = empid;
SELECT
emptype,
CASE
WHEN emptype = 'P' THEN doctype
ELSE 'No Documents required'
END
FROM
dms_report
WHERE
pilot = 1;
End//
DELIMITER ;
I have created this procedure successfully but when I try to call it, I am getting error 1305 the function database.temp does not exist. I am trying to call using this statement:
SET #increment = '1';
select temp( #increment)
but I get Error, please tell me where I made mistake.
This is how you call it, use use the keyword call and then procedure's name
call procedureName(params);
in call of making an string
String sqlString = "procedureName("+?+")"; //in case of Integers
String sqlString = "procedureName('"+?+"')";//in case of Integers
bring the parameter in prepared statement.
MySQL's documentation on Using JDBC CallableStatements to Execute Stored Procedures explains the necessary steps quite well.
This is what your java code needs to look like:
CallableStatement cStmt = conn.prepareCall("{call temp(?)}");
cStmt.setInt(1, 42); //set your input parameter, empId, to 42.
If you want to work with the rows returned by your stored procedure's query in your Java code, you're also going to need to create an OUT parameter as noted in MySql's documentation page titled, CALL Syntax:
CALL can pass back values to its caller using parameters that are
declared as OUT or INOUT parameters
In order to call your stored procedure from MySQL workbench, use the CALL command. You can call stored procedure by directly setting values for each of the parameters:
SET #increment = 1;
CALL temp(#increment)
Then you simply use the SELECT statement to return the value of your output parameter
SELECT #outParameter
With help setting your output parameters, please read the article MySQL Stored Procedure - SELECT - Example.
Your stored procedure is syntactically wrong, and as mentioned in the comments, you're not using the stored procedure functionality for it's intended use. It's intended to be used for data manipulation not for querying. You should instead consider turning your procedure into a series of prepared statements.
Please let me know if you have any questions!
I'm struck with an issue where am trying to call an Oracle stored procedure using Hibernate as in the below snippets.
My DAO class:
Query q = session.createSQLQuery(" {call PKG.PROC_GET_DATA_SET(?, :parameter1, :parameter2) }")
.setParameter(0, OracleTypes.CURSOR)
.setParameter("parameter1", "fDate")
.setParameter("parameter2", "tDate");
resultSet = q.list();
PROCEDURE:
CREATE OR REPLACE PACKAGE BODY schema.PKG
AS
PROCEDURE PROC_GET_DATA_SET(
P_CURSOR OUT SYS_REFCURSOR,
P_STRING1 IN VARCHAR2,
P_STRING2 IN VARCHAR2
)
AS
BEGIN
OPEN P_CURSOR FOR
.
.
.
But when i call the proc as in the DAO class, am getting an error as below.
Error:
PLS-00306: wrong number or types of arguments in call to 'PROC_GET_DATA_SET'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Struggling to spot the reason. Can someone throw some light here please?
TIA,
You cannot use this code to call a procedure using hibernate. See docs
The recommended call form is standard SQL92: { ? = call
functionName() } or { ? = call
procedureName(}. Native call syntax is not supported.
For Oracle the following rules apply:
A function must return a result set. The first parameter of a
procedure must be an OUT that returns a result set. This is done by
using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to
define a REF CURSOR type. See Oracle literature for further
information.
I suggest trying this:
{? = call PKG.PROC_GET_DATA_SET(?, ?) }
If this does not work, use session.connection()
I have written some code calling the mysql procedure in jdbc
Driver requires declaration of procedure to either contain a '\nbegin' or '\n' to follow argument declaration, or SELECT privilege on mysql.proc to parse column types.
am getting above error when i call procedure below is the code:
CallableStatement proc;
if (JDBCConnection.connection != null) {
Connection con = JDBCConnection.getConnection();
System.out.println("in DAOimpl "+username);
proc= con.prepareCall("{call getChildList(?) }");
System.out.println("prosedure"+proc);
proc.setString(1,username);
System.out.println(proc);
ResultSet rs=proc.executeQuery();
}
please give me the solution it is help ful for me.
The error message is quite clear:
Driver requires declaration of procedure to either contain a '\nbegin' or '\n' to follow argument declaration, or SELECT privilege on mysql.proc to parse column types.
So that tells us:
Your stored procedure declaration doesn't contain \nbegin or doesn't have an \n after the argument(s) declaration; or
The user you're using to call the statement doesn't have SELECT privilege on the stored procedure.
So step 1 is to figure out which of those two things it is.
To fix #1, put the body of the sproc in begin and end. So for instance, if your sproc is currently (altering an example from the MySQL docs a bit):
CREATE PROCEDURE proc(IN inputParam VARCHAR(255))
SELECT CONCAT('zyxw', inputParam);
Change it to
CREATE PROCEDURE proc(IN inputParam VARCHAR(255))
BEGIN
SELECT CONCAT('zyxw', inputParam);
END
To fix #2, grant the SELECT privilege on the sproc to the user.
I have written a Stored proc which calculates size of a table.
create or replace PROCEDURE RETRIEVE_TABLE_SIZE (
p_segment_type in VARCHAR2,
p_segment_name in VARCHAR2,
P_table_size out INTEGER )
AS
BEGIN
SELECT bytes/(1048576*1024) as GB FROM DBA_SEGMENTS WHERE SEGMENT_NAME = p_segment_type AND SEGMENT_TYPE = p_segment_name ;
END ;
I am calling this Stored proc from my java class and my code is below
String sqlQuery = "{call RETRIEVE_TABLE_SIZE(?,?,?)}";
CallableStatement callableStatement = connection.prepareCall(sqlQuery);
callableStatement.setString("p_segment_type","TABLE");
callableStatement.setString("p_segment_name","SIM_HEADER");
callableStatement.registerOutParameter("P_table_size",java.sql.Types.INTEGER);
callableStatement.executeUpdate();
Integer size = callableStatement.getInt(1);
System.out.println("size is: "+size);
But I am getting error given below:
Exception in thread "main" java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'RETRIEVE_TABLE_SIZE' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
This is first time I am writing Stored Proc. Please rectify me if I am doing something wrong.
A few notes, first check out this stackoverflow question that is essentially the same. Basically, from sqldeveloper command console you will be able to run the query, but the owner of the procedure probably doesn't have the proper permissions, or they are via a Role and not direct.
Second, from the Oracle documentation on the topic, you need to indicate within your stored procedure the value of the output parameter P_table_size, like so:
SELECT bytes/(1048576*1024) INTO P_table_size FROM DBA_SEGMENTS WHERE SEGMENT_NAME = p_segment_name AND SEGMENT_TYPE = p_segment_type ;
Also note that your use of p_segment_name and p_segment_type are reversed; you had SEGMENT_NAME = p_segment_type and SEGMENT_TYPE = p_segment_name which will cause no end of confusion when you resolve the permissions problem and get your query running.
One final note, although this is speculative: you may be able to use FROM USER_SEGMENTS instead of FROM DBA_SEGMENTS. According to the docs it has the same layout and data, but only returns data for the current user, which might bypass the permissions problem.