Solved

Stored Procedure ? marks less than the actual variables

Posted on 2002-04-10
11
199 Views
Last Modified: 2010-03-31
Here is the code, which has 8 question marks(?), and actual variables mapped are 9. So how the 9th variable are mapped?

What is the relation between question mark(?) and set method variables.


Code is....

sp = conn.prepareCall(" {?= call " sp_name(?,?,?,?,?,?,?,?)}");

            sp.registerOutParameter( 1, Types.INTEGER );

            sp.setInt(2, var1);
            sp.setInt(3, var2);
            sp.setInt(4, var3);
            sp.setInt(5, var4);

            sp.setInt(6, var5);

            sp.setInt(7, var6);
            sp.setInt(8, var7);

            sp.registerOutParameter (9, Types.VARCHAR );

            ResultSet rs = sp.executeQuery();

end....
0
Comment
Question by:virendra032700
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6930982
The escape syntax of the SQL you posted includes a result parameter. In other words, the stored procedure is a "function" that takes returns a result. The returned result is parameter number 1. The stored procedure also accepts 8 arguments. The first seven are input parameters -- they procedure expects you to provide a value when you call it. The last parameter is registered as an output parameter so the stored procedure expects to be able to set it and you  retrieve the parameter value after executing the call. So there really are 9 parameter markers which must be bound for this call -- one result parameter and eight formal arguments to the stored procedure.

Best regards,
Jim Cakalic
0
 

Author Comment

by:virendra032700
ID: 6931062
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
 

Author Comment

by:virendra032700
ID: 6931071
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:virendra032700
ID: 6931085
Hi Jim,

Thanks for ur quick reply. Could u pl map each ? with the variables.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931160
I hope this is what you are asking for ...

sp = conn.prepareCall(" {?= call sp_name(?,?,?,?,?,?,?,?)}");

// indicates return value of procedure is an int
sp.registerOutParameter(1, Types.INTEGER);

// next 7 set IN parameters to the procedure (all ints)
sp.setInt(2, var1);
sp.setInt(3, var2);
sp.setInt(4, var3);
sp.setInt(5, var4);
sp.setInt(6, var5);
sp.setInt(7, var6);
sp.setInt(8, var7);

// this tells the driver what kind of OUT parameter to expect
// corresponds to last actual parameter of stored procedure call
sp.registerOutParameter(9, Types.VARCHAR);

// execute the call
sp.execute();

// get the value of the OUT parameters
int returnValue = sp.getInt(1);
String outString = sp.getString(9);

Jim
0
 

Author Comment

by:virendra032700
ID: 6931195
sp = conn.prepareCall(" {?= call sp_name

is this ? mark pointing to sp.registerOutParameter( 1, Types.INTEGER ); in the code
or
the actual parameters in function ().
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6931219
The 1st question mark is parameter #1. The parameter indexes are the same for registerOutParameter, the setXXX methods, and the getXXX methods. When you call registerOutParameter(1, ...), you are telling the driver that the 1st parameter marker (question mark) that it encounters in the SQL statement you just prepared is an OUT parameter of the specified type. So you count _all_ the parameter markers, not just those that match the formal arguments declared by the stored procedure.

To summarize, the '?' that appears before the '=' is parameter #1. It is the 'target' of the registerOutParameter(1, Types.INTEGER) call.

Jim
0
 

Author Comment

by:virendra032700
ID: 6931230
Jim,

Thanks again. If there are more out parameters can we put more than one ? marks before '=' sign.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931247
There can only be one return value from a stored procedure -- just like there can only be one return value from a Java method. So you can't have multiple parameter markers preceding the '=' sign. However, even in your example you have more than one OUT parameter -- the first is the return value of the procedure and the other is a formal parameter that the procedure sets as part of its execution. You bound it as

    sp.registerOutParameter(9, Types.VARCHAR);

For multiple values, a stored procedure could also return a ResultSet. If it does it in the traditional manner, then you would use the executeQuery method to execute the call so that you could get a ResultSet object. A less-traditional manner is for the stored procedure to return the ResultSet as an OUT parameter. That tends to get a bit nasty and vendor-specific but it can be done.
0
 

Author Comment

by:virendra032700
ID: 6931330
Jim,

Thank you very much for your help as I was looking for the help desperately. Let me churn some of the points u told. If there is something I will write again.

Warm Regards,

Virendra
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6931343
You're welcome. Glad I could help.
Jim
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
throw exception 21 76
ejb mdb examples 1 36
jmss example java 2 46
How to configure empty element in XML Document parser? 15 57
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question