0

I have an arraylist of strings

ArrayList myList = new ArrayList(); myList = [url1,url2,url3]; 

I need to insert these urls in 3 different rows in the database.This is how I am doing it.

while(myList.size()!=0) { //get individual values in the array list int idx=0; String url= myList.get(idx++).toString() ; String insert="INSERT into test (url) values (?)"; prepstmt = conn.prepareStatement(insert); prepstmt .setString(r++, url); prepstmt.executeUpdate(); } 

This goes to infinite loop. Can someone please help me correct my code? Insert part of code is fine. But I am failing to get the individual urls.

Thanks!

4
  • 1
    Can you show us the loop part? Commented Mar 29, 2013 at 19:32
  • The code you've shown can't possibly go into an infinite loop. Please post the relevant part of your real code. Commented Mar 29, 2013 at 19:33
  • I used a while loop instead of if sorry.. Commented Mar 29, 2013 at 19:42
  • Your while loop should run forever. Your condition is on the size of the array and nothing in your loop changes this value. Commented Mar 29, 2013 at 19:56

3 Answers 3

2

Try this:

String insert = "INSERT into test (url) values (?)"; for (String url : myList) { prepstmt = conn.prepareStatement(insert); prepstmt.setString(1, url); prepstmt.executeUpdate(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I have put this code in my method. I do not have main() in this class. How do I test this method ?
make a main method and call your method there. Since main is static you should either make the method which inserts the rows static, or make an object of this class and call the method on that object.
1

@niculare give the best way you need to change your code.

but if you don't want to to use for-loop you need to change your if-statement like this:

int idx=0; while(idx < myList.size()) { String url= myList.get(idx).toString() ; String insert="INSERT into test (url) values (?)"; prepstmt = conn.prepareStatement(insert); prepstmt .setString(r++, myURL); idx++; } 

or if you not need this list any more use remove method:

while(myList.size()!=0) { //get individual values in the array list int idx=0; String url= myList.remove(idx++).toString() ; String insert="INSERT into test (url) values (?)"; prepstmt = conn.prepareStatement(insert); prepstmt .setString(r++, myURL); } 

you can use listIterator:

 ListIterator<String> iter = myList.listIterator(); while(iter.hasNext()){ String url = (String) iter.next(); String insert="INSERT into test (url) values (?)"; prepstmt = conn.prepareStatement(insert); prepstmt .setString(r++, myURL); } 

but I repeat once more: the best way is to use for-loop.

And one more advice. create you list like this:

List<String> myList = new ArrayList<String>(); 

UPDATE

this is one of my examples:

public class test2 { public static void main(String ... args) { ArrayList<String> myList = new ArrayList<String>(); myList.add("123"); myList.add("245"); myList.add("678"); ListIterator<String> iter = myList.listIterator(); while(iter.hasNext()){ String url = (String) iter.next(); System.out.println(url); } } 

7 Comments

I do not have main() in this class. How do I test this piece of code?
@smiley change your code to one of this. or you can write test app like in my update.
I used your iterator method. I initialized int r=1 . When I run the code, this is the error I see====> The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use.. SQLCODE=-302, SQLSTATE=22001, DRIVER=3.57.110
I think you get this error because you didn't process your preparedStatement. in your while-loop(or in for-loop if you use @nicular example) you need to add prepstmt.executeUpdate();
@smiley also I don't understand why you use r increment. You have simple query with one value and each time your set method must look like this: prepstmt .setString(1, myURL);. And you increment it and that why you get this exception
|
0

Posting full stack trace:

[3/30/13 2:35:00:241 GMT-06:00] 00000251 SystemOut O com.ibm.dw.register.PubHandler handleMessage(String promoter, String topic, String message) com.ibm.db2.jcc.am.eo: The value of a host variable in the EXECUTE or OPEN statement is out of range for its corresponding use.. SQLCODE=-302, SQLSTATE=22001, DRIVER=3.57.110

3 Comments

look at this post. There are may be two reasons : first - you trying to insert data to large for url or you have some problems with connection
I recomend you to close this question(or accept any answer) and write another question. May be someone can help you. i can't help because I don't work with IBM systems
Thanks!, the post helped. It was size of the database column I had to adjust.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.