File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ArrayIndexOutOfBoundsException: 1

 
Reshma Rangarajan
Greenhorn
Posts: 8
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to do an insert into table using PreparedStatement. I get a java.lang.ArrayIndexOutOfBoundsException: 1 exception at pStatement.setString(2, id);. Here's the code and stack trace. Can someone please let me know whats the bug in this code. Thanks in advance

[/code][/code]



java.lang.ArrayIndexOutOfBoundsException: 1
at oracle.jdbc.dbaccess.DBDataSetImpl._getDBItem(DBDataSetImpl.java:378)

at oracle.jdbc.dbaccess.DBDataSetImpl._createOrGetDBItem(DBDataSetImpl.j
ava:781)
at oracle.jdbc.dbaccess.DBDataSetImpl.setBytesBindItem(DBDataSetImpl.jav
a:2449)
at oracle.jdbc.driver.OraclePreparedStatement.setItem(OraclePreparedStat
ement.java:1190)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedSt
atement.java:1610)
at com.comp.sample.datalayer.SampleInsert.testInsert(Unknown Sourc
e)
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell me what is billNumbers which you have used in for loop.
whether you checked in the line number 378.
put some logger or sop to check the value of id inside the else part.

Regards,
Sriram.V
 
Kuldip Shetty
Ranch Hand
Posts: 43
Eclipse IDE Java
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the select and Insert query statements are using the same preparedStatement variable pStatement. While creating the preparedStatement instance for insert query guess you forgot to intialise to a new preparedStatement variable (LHS is missing)



Kuldip
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A general suggestion. It would be better if you Prepare your SELECT and INSERT statements outside the loop. If you are preparing the statements inside the loop, then you are soft parsing the query for each iteration of the loop and there by loosing the primary advantage of prepared statement - prepare once, execute many times.




 
Jan Cumps
Bartender
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Summarizing the great advise from the replies you received:
 
Reshma Rangarajan
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was spot on. Initializing the second prepareStatement solved my issue.

iStatement = conn.prepareStatement(insertString);

I have used two different prepared statements now for query and insert.

PS: billNumbers was an array of 'Long' values.

Thanks a lot to all of you for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic