File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error When Entering Data

 
Wayne Turner
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can enter data into my table using the traditional executeUpdate() method. So I know I'm not completely crazy.
But I want to get with the modern world. I'm trying to use a ResultSet to enter data but I keep getting an error. Here's my code:
==============================================

=================================================
I've double checked the data types. They are fine. The error is thrown when it reaches rsTask.insertRow(). I get the following error. (printStackTrace)
java.lang.ArrayIndexOutOfBoundsException: 1 void sun.jdbc.odbc.JdbcOdbcResultSet.bindCol(int, int) JdbcOdbcResultSet.java:4506 void sun.jdbc.odbc.JdbcOdbcResultSet.insertRow() JdbcOdbcResultSet.java:3962 void mytagtest.NewTask.doPut(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) NewTask.java:107 void
.....and so on.
As far as I can see, I am doing just like it is recommended in the tutorial.
What do you think is the problem?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I want to get with the modern world

Hmmm... that's the first I have ever heard that executeUpdate is outdated?
A couple things...
1. If your only intent in your code block is to insert a new row then I would not recommend the overhead of actually running the query as what happens with .executeQuery() if you are not going to process the rs. I would use executeUpdate() or do it as a PreparedStatement instead.
2. Not sure if this is true with all dbs but ours (Informix) requires that the table have "rowids" in order to use an updatable rs. The "rowid" is a hidden column that the db uses to uniquely identify a row (like a serial field you don't normally see).
Hope that helps.
 
Wayne Turner
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, maybe it's not the modern world. I'm just hacking around trying to find something that looks clean and works.
This updatable ResultSet looked interesting because you didn't have to concatenate some ugly SQL. I also cut my teeth on MS Access, and it looked like the ol' DAO stuff. A bit familiar.
And, yes, since I'm hacking around, I'm using an Access db. Eventually I'll use Oracle. I also have an autonumber field in the table that creates a unique number for each record. And I made sure I have a recent ODBC driver.
But isn't it interesting I get an out of bounds array exception. It has to be something to do with java.sql.'something'. I have no array in my code. I was wondering if anyone had run into this. Is it a bug? Did I not intone the proper mystic words? Maybe this is a feature that will work one day...but not now.
Anyone else seen this?
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to move the resultset to the first row before you can update it! Otherwise you are trying to update the beforeFirst row!
try this:
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a note: Scrollable resultsets may give you more headaches than they are worth. Scrollable resultsets are slower, use more memory, and do not work well when using odbc drivers. They also tend to be a database resource hog. But I'll give you that they look nice!
Jamie
 
Blake Minghelli
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to duplicate your error. I also tried it on our Informix db and I got a NullPointerException instead.
In both cases, it's the jdbc driver that's throwing the exception - somewhere in the driver's code, it's using an array. It sounds to me like it is a feature not fully implemented in the jdbc-odbc bridge driver.
If you're looking for cleaner code, I really suggest a PreparedStatement, especially if you are executing the same insert multiple times...

Good luck!
 
Wayne Turner
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, everyone, for your help. And Blake, thank you for going to the trouble of reporducing my error. I may be crazy, but I'm not crazy about this, at least.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"turncom",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic