PaulCaswell
asked on
executeUpdate returns -1?
All,
What can cause executeUpdate to return -1?
Paul
What can cause executeUpdate to return -1?
Paul
What was the query?
Shouldn't be anything that can cause that
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post the code?
Can you not just ignore it anyway? Since you shouldnt be expecting to recieve a value back from an update, if it does return a value, can you not just ignore it?
The only problem with this is that -1 would typiclally imply the update failed (although as CEHJ says, this is incorrect as to the Java spec).
The only problem with this is that -1 would typiclally imply the update failed (although as CEHJ says, this is incorrect as to the Java spec).
Instead of returning -1, you should expect an SQLException.
This value obviously indicate an error (maybe not well managed by the driver) :
- invalid sql query
- connection error
- other (disk is full, ...)
If you want an exception, then :
if (ps.executeUpdate()<0) throw new SQLException("unknown error");
This value obviously indicate an error (maybe not well managed by the driver) :
- invalid sql query
- connection error
- other (disk is full, ...)
If you want an exception, then :
if (ps.executeUpdate()<0) throw new SQLException("unknown error");
ASKER
All,
Cracked it!
Thanks for all the quick help!
The problem was twofold:
1) I was using:
makeTable.executeUpdate(qu ery);
collected = makeTable.getUpdateCount() ;
This works fine against a SQL2005 database but fails on SQL2000 (the getUpdateCount returns -1). I didnt realise that executeUpdate returned the count. :-(
This works:
collected = makeTable.executeUpdate(qu ery);
2) I make a temp table '#tempTable' using an INSERT INTO, then run a query against it. This works fine when driving SQL2005 Express. This went tilt against a SQL2000 database. A little research and experimentation shows that temp tables are handled slightly differently between the two versions. Using #tableName works fine in 2005 but you have to use ##tableName when running against 2000.
Note that the Java drivers are also different between the two versions so the difference may be in the driver rather than the database.
Again, thanks all for the suggestions.
I need to think about points distribution. No time now. Back later. If you have any preferences, let me know.
Paul
Cracked it!
Thanks for all the quick help!
The problem was twofold:
1) I was using:
makeTable.executeUpdate(qu
collected = makeTable.getUpdateCount()
This works fine against a SQL2005 database but fails on SQL2000 (the getUpdateCount returns -1). I didnt realise that executeUpdate returned the count. :-(
This works:
collected = makeTable.executeUpdate(qu
2) I make a temp table '#tempTable' using an INSERT INTO, then run a query against it. This works fine when driving SQL2005 Express. This went tilt against a SQL2000 database. A little research and experimentation shows that temp tables are handled slightly differently between the two versions. Using #tableName works fine in 2005 but you have to use ##tableName when running against 2000.
Note that the Java drivers are also different between the two versions so the difference may be in the driver rather than the database.
Again, thanks all for the suggestions.
I need to think about points distribution. No time now. Back later. If you have any preferences, let me know.
Paul
As you found the solution, you can ask refunding the points.
ASKER
>> As you found the solution, you can ask refunding the points.
Is anything here of value to PAQ? I am no Java guru. Is this just noise or valuable? I wont be offended :-)
Paul
Is anything here of value to PAQ? I am no Java guru. Is this just noise or valuable? I wont be offended :-)
Paul
i think it should be PAQd, the soltuion is here...
> Note that the Java drivers are also different between the two versions so the difference may be in the driver rather than the database.
I actually mentioned that it was dependant on the driver ;)
I actually mentioned that it was dependant on the driver ;)
do as you feel fit though, doesn't bother me :)
The problem was nothing to do with the driver. It was due to collecting the result of getUpdateCount instead of executeUpdate
ASKER
>> The problem was nothing to do with the driver. It was due to collecting the result of getUpdateCount instead of executeUpdate
We have no way of knowing if it was the driver or SQL. getUpdateCount works with the SQL2005 and its driver while it returns -1 with SQL2000 and its driver. Both right! Split points! Sorry for the B.
Paul
We have no way of knowing if it was the driver or SQL. getUpdateCount works with the SQL2005 and its driver while it returns -1 with SQL2000 and its driver. Both right! Split points! Sorry for the B.
Paul
>>getUpdateCount works with the SQL2005 and its driver while it returns -1 with SQL2000 and its driver.
Yes, but the question was about why *executeUpdate* returns -1
>>What can cause executeUpdate to return -1?
and the following code:
>>
makeTable.executeUpdate(qu ery);
collected = makeTable.getUpdateCount() ;
>>
clearly shows, as i mentioned in my last comment, the result being collected from getUpdateCount (which *can* return -1) and *not* executeUpdate
Yes, but the question was about why *executeUpdate* returns -1
>>What can cause executeUpdate to return -1?
and the following code:
>>
makeTable.executeUpdate(qu
collected = makeTable.getUpdateCount()
>>
clearly shows, as i mentioned in my last comment, the result being collected from getUpdateCount (which *can* return -1) and *not* executeUpdate