• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.ArrayIndexOutOfBoundsException: 1

 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Summarizing the great advise from the replies you received:
 
Reshma Rangarajan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic