Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Best practice for getting ResultSet from MS SQL stored procedure?

Posted on 2007-10-04
4
Medium Priority
?
493 Views
Last Modified: 2008-01-09
Dear Experts,

Due to technical difficulties, I'm trying to circumvent a PreparedStatement and instead generate and use an MS SQL stored procedure directly.  However, I've never done it in Java.

I know how to write the stored procedure itself.  But what is the preferred syntax (other than PreparedStatement) to call it and return the ResultSet?

Thanks!
BrianMc1958
0
Comment
Question by:BrianMc1958
4 Comments
 
LVL 20

Accepted Solution

by:
gatorvip earned 1000 total points
ID: 20013646
0
 
LVL 35

Expert Comment

by:TimYates
ID: 20013701
I just use PreparedStatements with MySQL and Postgres

Maybe that works in MSSQL as well (I've never tried)

     prepareStatement(  "CALL my_function( ?, ? )" ) ;

for example...
0
 
LVL 9

Assisted Solution

by:brunoguimaraes
brunoguimaraes earned 1000 total points
ID: 20013889
CallableStatement is the way to go.

Suppose the procedure returns an int value and that the first two parameters are IN (String and int), and the last is OUT (float). Then you should do:

callableStatement  = connection.prepareCall(" { ? = CALL procname(?, ?, ?) }");

callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
callableStatement.setString(2, "string");
callableStatement.setInt(3, 100);
callableStatement.registerOutParameter(4, java.sql.Types.FLOAT);

Then, after you execute the procedure, you can retrieve the return value and the out parameter value.

resultSet = callableStatement.executeQuery();

int returnValue = callableStatement.getInt(1); // get the return value
float outParameter = callableStatement.getFloat(4); // get the out parameter value

0
 

Author Comment

by:BrianMc1958
ID: 20014135
Thanks a lot, folks.  Works very nicely.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

580 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