Read the following vis a vis PreparedStatement Interface:
"Some prepared statements return multiple results;"
Can't fathom how a single prepared statement might return multiple
resultsets. Could someone site a practical example to help me understand
this.
Also, came across the following piece of code in a sample stored
procedure:
rs1[0] = stmt.executeQuery();
where rs1 is a ResultSet object and is a PreparedStatement object. While I
understand that stmt.executeQuery() will return an Object of type
ResultSet, what does "[0]" in
rs1[0] mean? Normally, in Java, [0] indicates the first element in the
array. So, what does it mean to store the resultset as the first element
in the array.
TIA
Raquel.
Roedy Green - 15 Jun 2004 08:02 GMT
>"Some prepared statements return multiple results;"
I could see two interpretations:
1. you get multiple rows back.
2. you ran reuse the PreparedStatement for another query.
I don't see how you could get multiple ResultSets literally back from
the same query since the method signature does not allow for it.

Signature
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Joe Weinstein - 15 Jun 2004 16:30 GMT
> Read the following vis a vis PreparedStatement Interface:
>
[quoted text clipped - 3 lines]
> resultsets. Could someone site a practical example to help me understand
> this.
Hi. A single prepared statement or even a plain statement can easily
return multiple result sets for a single execution, if the DBMS will
cooperate. Sybase and MS for example:
create procedure myproc as
begin
select * from mytable1
update mytable2 set foo = 'bar'
select * from mytable3 where foo = 'bar'
end
This procedure will return two result sets with an update count
in the middle. The jdbc to process this procedure and
get all the output is:
ResultSet r = s.executeQuery("myproc");
while (r.next())... // fully process first result set
r.close();
s.getMoreResults();
int myUpdateCount = s.getUpdateCount();
s.getMoreResults();
r = s.getResultSet(); // get second result set
while (r.next()) ...
r.close();
The canonical form for a callable statement handling an arbitrarily
complex procedure is shown below.
CallableStatement cstmt1 = conn.prepareCall("{ ? = call sp_myname(?)}");
cstmt1.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt1.registerOutParameter(2, java.sql.Types.VARCHAR);
boolean firstReturnIsAResultSet = cstmt1.execute();
while (true)
{
ResultSet rs = cstmt1.getResultSet();
int updateCount = cstmt1.getUpdateCount();
// If there are no more results or counts, we're done.
if (rs == null && updateCount == -1)
break;
// Check to see if there is a ResultSet
if (rs != null) {
while (rs.next()) {
System.out.println("Get first col by id:" + rs.getString(1));
}
rs.close();
} // Otherwise, there will be an update count
else {
System.out.println("Update count = " + updateCount);
}
cstmt1.getMoreResults();
}
// Best to retrieve status after all result sets and update counts
// have been retrieved.
System.out.println( "Output status: " + cstmt1.getInt(1));
System.out.println( "Output param: " + cstmt1.getString(2));
cstmt1.close();
Joe Weinstein at BEA
> Also, came across the following piece of code in a sample stored
> procedure:
[quoted text clipped - 10 lines]
> TIA
> Raquel.
Raquel - 16 Jun 2004 12:22 GMT
Excellent example Joe and thank you so much for this detailed explanation.
I am actually working on UDB which (I think) does not support this kind of
statement grouping.
Thanks,
Raquel.
Joe Weinstein - 15 Jun 2004 16:32 GMT
Oh, and for Oracle procedures, which can't return results inline like fresh SQL,
they provide for cursors as output parameters for procedures, so you can get
multiple result sets from a given procedure execution by doing a getObject()
for the output parameters and casting them to result sets.
Joe Weinstein at BEA
> Read the following vis a vis PreparedStatement Interface:
>
[quoted text clipped - 18 lines]
> TIA
> Raquel.
Lee Fesperman - 15 Jun 2004 22:36 GMT
> Read the following vis a vis PreparedStatement Interface:
>
[quoted text clipped - 3 lines]
> resultsets. Could someone site a practical example to help me understand
> this.
As Joe described, a stored procedure call can return multiple results.
In addition, you might send multiple SQL commands to a single prepared statement, thus
you will receive multiple results (if supported by the backend DBMS). For example:
prepStnt = conn.prepareStatement("SELECT*FROM tab1;SELECT*FROM tab2");
... this will produce 2 resultsets (for brevity, I didn't use ? parameters, though they
are allowed.)
Note: the multiple commands can be a mixture of queries, updates and control commands.

Signature
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)