The Artima Developer Community
Sponsored Link

Java Answers Forum
help with this preparedStatment

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

help with this preparedStatment Posted: Jan 20, 2003 10:42 AM
Reply to this message Reply
Advertisement
Hello everyone
It is not alot of code but I am having great differculties with it.
I have another integer which is the value 4.
And a arraylist with 40 words inside it.
Now I can set up my column names and question marks to 4 without having to chnaging my sql statement.
I want to be able to get the first 10 values from my arraylist (arr3) and insert them into the first column name (Phone).
Then i want the next 10 words from the arraylist (10 to 20) and Insert them into the second column name(Addr1).
Then I want the next 10 words from the arraylist (20 to 30) and Insert them into the Third column name(Addr2).
Then I want the next 10 words from the arraylist (30 to 40) and Insert them into the last column name (Addr3).
As you can see below that my code will do this fine, But my problem is that i want to be able to do this without having to change my code each time i have another setString method to fill.
I want to be able to have just one setString method so that each time it loops though it will change the values to what is there.
for example
pss.setString("loop Though so that it will change the number 1 to a 2 then to a 3 then 4", arr3.get(j1+ "then another loop so that it will add 10 to j1 each time it loops though").toString())
Thanks for your help and time
Ben

Here is my code that works fine

// arr3 is a arraylist that holds 40 words inside it.
try {
String data = "jdbc:odbc:myProject";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data,"","");



String sql = "INSERT INTO Ben (Phone,Addr1,Addr2,Addr3) VALUES (?,?,?,?)";

PreparedStatement pss = conn.prepareStatement(sql);
for(int j1 = 0; j1 < 10; j1++){

pss.setString(1, arr3.get(j1).toString());
pss.setString(2, arr3.get(j1+10).toString());
pss.setString(3, arr3.get(j1+20).toString());
pss.setString(4, arr3.get(j1+30).toString());
pss.executeUpdate();
}



pss.close();
return;

}catch (Exception e1) {
System.err.println("Got an exception! ");
System.err.println(e1.getMessage());


}

Topic: Raising Events from Classes Previous Topic   Next Topic Topic: Java Array Coursework

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use