File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes java.lang.ArrayIndexOutOfBoundsException: 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.lang.ArrayIndexOutOfBoundsException: 1" Watch "java.lang.ArrayIndexOutOfBoundsException: 1" New topic
Author

java.lang.ArrayIndexOutOfBoundsException: 1

Reshma Rangarajan
Greenhorn

Joined: Nov 02, 2010
Posts: 8
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

Joined: Nov 20, 2010
Posts: 109
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


For java examples,ebooks,interview questions,visit this blog
http://periodicupdates.blogspot.com/
Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 39

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

Joined: Feb 22, 2011
Posts: 75
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

Joined: Dec 20, 2006
Posts: 2491
    
    8

Summarizing the great advise from the replies you received:


OCUP UML fundamental and ITIL foundation
youtube channel
Reshma Rangarajan
Greenhorn

Joined: Nov 02, 2010
Posts: 8
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.
 
Consider Paul's rocket mass heater.
 
subject: java.lang.ArrayIndexOutOfBoundsException: 1
 
Similar Threads
creating website basic question
return JRResultSetDataSource from a DAO ... unable to get next record
Java-JSP
Pass bean from one action to another in strust2
Jdbc - inserting records-exception(connection reset).