Results 1 to 11 of 11
- 12-10-2012, 01:23 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Putting array of numbers in SQL (WHERE) code
Hey, i got an sql code that puts data from database into my table.
I also got a list array of numbers.
When i write this:
System.out.println(list.get(i)); - It gives me all the values of that array (1,2,3,4 etc).
I want to put this array in my sql code, like this:
"SELECT * FROM Table1 WHERE Id='" + list.get(i) + "'";
This sql code only displays 1 value. I want it to display ALL values, not just one.
If i write:
"SELECT * FROM Table1 WHERE Id='" + list.get(0) + "' OR '" + list.get(1) + '";
It would display 1st and 2nd value.
Why isnt the list.get(i) working, and how do i make it work?
- 12-10-2012, 01:34 PM #2
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Putting array of numbers in SQL (WHERE) code
yes u couldn't store array values directly into database. so u will be append the array values into stringbuffer after that u convert string buffer into string.
Now u store this string into ur database. and also compare this string to ur db..
- 12-10-2012, 02:14 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Putting array of numbers in SQL (WHERE) code
Please do not ask for code as refusal often offends.
- 12-10-2012, 02:21 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Re: Putting array of numbers in SQL (WHERE) code
thank you, ill see if i can make it work :)
Edit:
hmm it still seems to display just one value from database, i think its because my arraylist displays numbers like this:
1
2
3
4..
(in a column)
Can i make it so it would display them in a line? one after each other with a ',' in between them? like this: 1,2,3,4Last edited by iNko; 12-10-2012 at 02:28 PM.
- 12-10-2012, 02:40 PM #5
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Putting array of numbers in SQL (WHERE) code
i think if u want this..
String s={1,2,3,4};
u just compare this string into db
SELECT * FROM <your table> WHERE Id=s.
or if u have any unique id u use that into ur comparison.. i think this will be help to u..
- 12-10-2012, 02:59 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
- 12-10-2012, 02:59 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
- 12-10-2012, 03:11 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Re: Putting array of numbers in SQL (WHERE) code
This is the method where i get my ArrayList (its values from another table):
As a test, i write "System.out.print(list.get(i));" and it shows the numbers that i need, so array is working normally i assume.Java Code:private void arraylistValues(){ try { String sql = "SELECT table2_id FROM Table2; pst = conn.prepareStatement(sql); rs = pst.executeQuery(sql); while (rs.next()){ list.add(rs.getString("table2_id ")); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }
And this is the method that populates my table:
Note that if in this method in SQL code i write "SELECT * FROM Table1 WHERE id IN (1,2,3,4,5)"; , it would display all 5 values in my jTable, but if i write "IN (" + list.get(i) + ")"; it still only displays 1 last valueJava Code:private void updateTable(){ try{ int totalElements = list.size(); for(int i = 0; int totalElements< 2; i++ ) { String sql = "SELECT * FROM Table1 WHERE id IN (" + list.get(i) + ")"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(sql); jTable1.setModel(DbUtils.resultSetToTableModel(rs)); } } catch (Exception e){ JOptionPane.showMessageDialog(null, e); }
Edit: I think i just need somehow change list.get(i), to show numbers like this - 1,2,3,4,5Last edited by iNko; 12-10-2012 at 03:14 PM.
- 12-10-2012, 03:20 PM #9
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Putting array of numbers in SQL (WHERE) code
- 12-10-2012, 03:21 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Putting array of numbers in SQL (WHERE) code
You would concatenate the IN clause:
Note you'll have to lose the last comma...:)Java Code:StringBuilder sb = new StringBuilder(); for (String s : list) { sb.append(s); sb.append(","); }
BUT...if this list of ids is simply the contents of table2, then this is a single query:
Java Code:SELECT * FROM table1 WHERE id IN (SELECT table2_id FROM table2)
Please do not ask for code as refusal often offends.
- 12-10-2012, 03:35 PM #11
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
Putting the content (code) for each tab into a different class
By Ami in forum AWT / SwingReplies: 1Last Post: 07-26-2012, 09:48 PM -
Reading input from a .txt file and then putting it into a array
By BetaDave1877 in forum New To JavaReplies: 2Last Post: 04-16-2012, 12:56 AM -
Taking values out of one array and putting them into another.
By Wnt2bsleepin in forum New To JavaReplies: 22Last Post: 02-05-2012, 10:46 PM -
I need help with putting a logo on my code
By MikeJ39 in forum New To JavaReplies: 13Last Post: 10-22-2011, 01:10 AM -
Putting code together.
By newbee in forum New To JavaReplies: 3Last Post: 04-17-2008, 03:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks