Solved

executeUpdate returns -1?

Posted on 2006-07-05
16
762 Views
Last Modified: 2008-02-01
All,

What can cause executeUpdate to return -1?

Paul
0
Comment
Question by:PaulCaswell
  • 4
  • 3
  • 3
  • +3
16 Comments
 
LVL 14

Expert Comment

by:StillUnAware
Comment Utility
What was the query?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Shouldn't be anything that can cause that
0
 
LVL 92

Assisted Solution

by:objects
objects earned 62 total points
Comment Utility
technically it shouldn't, but depends on the driver.
what driver ru using?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 63 total points
Comment Utility
It would have to be a driver that is incorrect vis-a-vis the spec
0
 
LVL 8

Expert Comment

by:colr__
Comment Utility
Can you post the code?
0
 
LVL 8

Expert Comment

by:colr__
Comment Utility
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).
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
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");
0
 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
All,

Cracked it!

Thanks for all the quick help!

The problem was twofold:

1) I was using:

          makeTable.executeUpdate(query);
          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(query);

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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
As you found the solution, you can ask refunding the points.
0
 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
>> 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
0
 
LVL 8

Expert Comment

by:colr__
Comment Utility
i think it should be PAQd, the soltuion is here...
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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 ;)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
do as you feel fit though, doesn't bother me :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The problem was nothing to do with the driver. It was due to collecting the result of getUpdateCount instead of executeUpdate
0
 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
>> 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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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(query);
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now