Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-02-2010, 03:48 AM
Member
 
Join Date: Feb 2010
Posts: 2
Rep Power: 0
plica10 is on a distinguished road
Default JDBC problem with multiple resultsets on mysql database: getUpdateCount() always zero
I'd be grateful for anyones help please

I have written a java class to execute a MySQL stored procedure that does an UPDATE and then a SELECT. I want to handle the resultset from the SELECT AND get a count of the number of rows updated by the UPDATE.

Even though several rows get updated by the stored proc, getUpdateCount() always returns zero. It's like following the UPDATE with a SELECT causes the updatecount info to be lost. I tried it in reverse: SELECT first and UPDATE last and it works properly. I can get the resultset and the updatecount.

My Stored Procedure:

Code:
delimiter $$
CREATE PROCEDURE multiRS(
			IN drugId int,
        		IN drugPrice decimal(8,2)
			)
BEGIN
	UPDATE drugs
        SET DRUG_PRICE = drugPrice
        WHERE DRUG_ID > drugId; 
	
        SELECT DRUG_ID, DRUG_NAME, DRUG_PRICE
		FROM Drugs where DRUG_ID > 7
		ORDER BY DRUG_ID ASC;                  	
END $$
In my program (below) callablestatement.execute() returns TRUE even though the first thing I do in my stored proc is an UPDATE not a SELECT. Is this at odds with the JDBC 2 API? Shouldn't it return false since the first "result" returned is NOT a resultset but an updatecount? Does JDBC return any resultsets first by default, even if INSERTS, UPDATES happened in the stored proc before the SELECTs??

Excerpt of my Java Class:
Code:
// Create CallableStatement
      CallableStatement cs = con.prepareCall("CALL multiRS(?,?)");

// Register input parameters
........

// Execute the Stored Proc
    boolean getResultSetNow = cs.execute();
    int updateCount = -1;
	System.out.println("getResultSetNow: " +getResultSetNow);
	
	while (true) {
			
		if (getResultSetNow) {
	      ResultSet rs = cs.getResultSet();
	      while (rs.next()) {
        	// fully process result set before calling getMoreResults() again!
		        System.out.println(rs.getInt("DRUG_ID")
		          +", "+rs.getString("DRUG_NAME")
		          +", "+rs.getBigDecimal("DRUG_PRICE")); 
		      }
	      	rs.close();
	    } else {
			updateCount = cs.getUpdateCount();
			if (updateCount  != -1) { // it's a valid update count
				System.out.println("Reporting an update count of " +updateCount);
			}
		}
		if ((!getResultSetNow) && (updateCount == -1)) break;
			// done with loop, finished all the returns
				getResultSetNow = cs.getMoreResults();
	}
The output of running the program at command line:
Code:
getResultSetNow: true

28, Apple, 127.00
35, Orange, 127.00
36, Bananna, 127.00
37, Berry, 127.00

Reporting an update count of 0

My Setup:
Mac OS X 10.3.9
Java 1.4.2
mysql database 5.0.19
mysql-connector 5.1.10 (connector/J)

Thanks for your help.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-02-2010, 12:23 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
If you were doing this in MySQL (however that works, I tend to be on Oracle) would you be able to get the update count?

JDBC does not specify how a call to a stored proc should work in terms of automatically returned resultsets and results (ie counts). After all, an Oracle stored proc only returns what is defined in the proc itself.

Why not simply return the count as an out parameter?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-02-2010, 02:26 PM
Member
 
Join Date: Feb 2010
Posts: 2
Rep Power: 0
plica10 is on a distinguished road
Default
Thanks for the response Tolls.

Just to recap:
I am using JDBC to connect to a MySql database. I have written a stored procedure that does an UPDATE and then a SELECT. Therefore I have to use the jdbc execute() method since there are multiple results returned to java due to the database update and query.

I have tested it from the MySql client and it does indeed update several rows of a table and then selects a few rows from that table. I have a java class that uses callablestatement and the execute() method of the JDBC api to call the stored proc and retrieve its "results" (i.e: A resultset and an Updatecount).

During my testing I have noticed:
1. According to the Java documentation execute() returns true if the first result is a ResultSet object; false if the first result is an update count or there is no result. In my java class callablestatement.execute() will return TRUE if in the stored proc the UPDATE is done first and then the SELECT last or vica versa.
2. My java class (above) is coded to loop through all results returned from the stored proc in succession. Running this class shows that any resultsets are returned first and then update counts last regardless of the order in which they appear in the stored proc. Maybe there is nothing unusual here, it may be that Java is designed to return any Resultsets first by default even if they didn't happen first in the stored procedure?
3. In my stored procedure, if the UPDATE happens last then callablestatement.getUpdateCount() will return the correct number of updated rows.
4. If the UPDATE is followed by a SELECT (see above) then callablestatement.getUpdateCount() will return ZERO even though rows were updated.
5. I tested it with the stored proc doing SELECT - UPDATE - SELECT and again getUpdateCount() returns ZERO.
6. I tested it with the stored proc doing SELECT - UPDATE - SELECT - UPDATE and this time getUpdateCount() returns the number rows updated by the last UPDATE and not the first.


I take your point about returning rowcounts in OUT parameters but I want my code to be modular enough to cover the situation where a statement may return more than one ResultSet object, more than one update count, or a combination of ResultSet objects and update counts. The sql may need to be generated dynamically, its statements may be unknown at compile time.

Maybe I have exposed a bug in the JDBC api?

Thank you for reading

Last edited by plica10; 02-02-2010 at 02:39 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-02-2010, 02:33 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
It won't be JDBC. It'll be the MySQL implementation of it you're using.

ETA: That is, of course, whether it is actually a bug in the first place.

Last edited by Tolls; 02-02-2010 at 02:36 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
getupdatecount, java, jdbc, multipleresultsets, mysql

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JDBC and mySQL database trouble ichwar Database 15 03-09-2010 12:30 AM
MySQL/JDBC prepared statement problem thelinuxguy Advanced Java 3 02-12-2009 12:21 AM
Mysql/JDBC update query problem thelinuxguy Advanced Java 3 02-11-2009 10:56 PM
problem in connecting to mysql database nancyv Java Servlet 6 04-02-2008 12:33 PM
Problem with jTable that is binded with a table in MySQL Database rajkenneth NetBeans 0 03-29-2008 04:36 PM


All times are GMT +2. The time now is 07:40 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org