how to put two fields result set in to array list in java
up vote
-2
down vote
favorite
Here is my code: there is one database table field I m retrieving, but I want the second one too.
String query = "select QId, Options from QuestionMaster where SurveyID = '" + s + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList list = new ArrayList();
while (rs.next())
{
list.add(rs.getString(1));
}
rs.close();
String contactListNames = (String) list.toArray(new String[list.size()]);
is there any way to do this by 2d arraylist or 2d array
java
|
show 3 more comments
up vote
-2
down vote
favorite
Here is my code: there is one database table field I m retrieving, but I want the second one too.
String query = "select QId, Options from QuestionMaster where SurveyID = '" + s + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList list = new ArrayList();
while (rs.next())
{
list.add(rs.getString(1));
}
rs.close();
String contactListNames = (String) list.toArray(new String[list.size()]);
is there any way to do this by 2d arraylist or 2d array
java
1
How second one is decided?
– user7294900
Nov 11 at 15:09
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21
|
show 3 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Here is my code: there is one database table field I m retrieving, but I want the second one too.
String query = "select QId, Options from QuestionMaster where SurveyID = '" + s + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList list = new ArrayList();
while (rs.next())
{
list.add(rs.getString(1));
}
rs.close();
String contactListNames = (String) list.toArray(new String[list.size()]);
is there any way to do this by 2d arraylist or 2d array
java
Here is my code: there is one database table field I m retrieving, but I want the second one too.
String query = "select QId, Options from QuestionMaster where SurveyID = '" + s + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList list = new ArrayList();
while (rs.next())
{
list.add(rs.getString(1));
}
rs.close();
String contactListNames = (String) list.toArray(new String[list.size()]);
is there any way to do this by 2d arraylist or 2d array
java
java
edited Nov 11 at 15:37
asked Nov 11 at 15:07
Faraz Naeem
15
15
1
How second one is decided?
– user7294900
Nov 11 at 15:09
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21
|
show 3 more comments
1
How second one is decided?
– user7294900
Nov 11 at 15:09
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21
1
1
How second one is decided?
– user7294900
Nov 11 at 15:09
How second one is decided?
– user7294900
Nov 11 at 15:09
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
If your ids are unique, you can use a HashMap to store the resultset:
Map< String, String> hm = new HashMap<String, String>();
while (rs.next()) {
hm.put(rs.getString(1), rs.getString(2));
}
Set<Map.Entry<String,String>> set = hm.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
entry.getKey() returns QId and entry.getValue() returns Options
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
add a comment |
up vote
0
down vote
Basically you create model class that fits to a table.
Each query you do have to return that exact type so you know the entry is correct.
(Won't go further but take a look at how Jpa or else works)
- you create a class with all attributes (id, option, name...), let
call this POJO - query your fields
- iterate over each result and
create your POJO Object - add it to the list
there is plenty of other thing you could do here but this is the beginning of a database mapping
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If your ids are unique, you can use a HashMap to store the resultset:
Map< String, String> hm = new HashMap<String, String>();
while (rs.next()) {
hm.put(rs.getString(1), rs.getString(2));
}
Set<Map.Entry<String,String>> set = hm.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
entry.getKey() returns QId and entry.getValue() returns Options
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
add a comment |
up vote
0
down vote
If your ids are unique, you can use a HashMap to store the resultset:
Map< String, String> hm = new HashMap<String, String>();
while (rs.next()) {
hm.put(rs.getString(1), rs.getString(2));
}
Set<Map.Entry<String,String>> set = hm.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
entry.getKey() returns QId and entry.getValue() returns Options
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
add a comment |
up vote
0
down vote
up vote
0
down vote
If your ids are unique, you can use a HashMap to store the resultset:
Map< String, String> hm = new HashMap<String, String>();
while (rs.next()) {
hm.put(rs.getString(1), rs.getString(2));
}
Set<Map.Entry<String,String>> set = hm.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
entry.getKey() returns QId and entry.getValue() returns Options
If your ids are unique, you can use a HashMap to store the resultset:
Map< String, String> hm = new HashMap<String, String>();
while (rs.next()) {
hm.put(rs.getString(1), rs.getString(2));
}
Set<Map.Entry<String,String>> set = hm.entrySet();
for (Map.Entry<String, String> entry : set) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
entry.getKey() returns QId and entry.getValue() returns Options
answered Nov 11 at 15:23
forpas
5,4111217
5,4111217
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
add a comment |
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
thanks for sharing but this is the only way to do this ?
– Faraz Naeem
Nov 11 at 15:32
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
The other way is to create a class with 2 fields and your list will store objects of this class.
– forpas
Nov 11 at 15:33
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
I need to have the example what you last suggested.
– Faraz Naeem
Nov 11 at 15:50
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
There are a lot of sources to study for Java classes. One of them: codejava.net/coding/…. You can google for more.
– forpas
Nov 11 at 15:54
add a comment |
up vote
0
down vote
Basically you create model class that fits to a table.
Each query you do have to return that exact type so you know the entry is correct.
(Won't go further but take a look at how Jpa or else works)
- you create a class with all attributes (id, option, name...), let
call this POJO - query your fields
- iterate over each result and
create your POJO Object - add it to the list
there is plenty of other thing you could do here but this is the beginning of a database mapping
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
add a comment |
up vote
0
down vote
Basically you create model class that fits to a table.
Each query you do have to return that exact type so you know the entry is correct.
(Won't go further but take a look at how Jpa or else works)
- you create a class with all attributes (id, option, name...), let
call this POJO - query your fields
- iterate over each result and
create your POJO Object - add it to the list
there is plenty of other thing you could do here but this is the beginning of a database mapping
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
add a comment |
up vote
0
down vote
up vote
0
down vote
Basically you create model class that fits to a table.
Each query you do have to return that exact type so you know the entry is correct.
(Won't go further but take a look at how Jpa or else works)
- you create a class with all attributes (id, option, name...), let
call this POJO - query your fields
- iterate over each result and
create your POJO Object - add it to the list
there is plenty of other thing you could do here but this is the beginning of a database mapping
Basically you create model class that fits to a table.
Each query you do have to return that exact type so you know the entry is correct.
(Won't go further but take a look at how Jpa or else works)
- you create a class with all attributes (id, option, name...), let
call this POJO - query your fields
- iterate over each result and
create your POJO Object - add it to the list
there is plenty of other thing you could do here but this is the beginning of a database mapping
answered Nov 11 at 15:30
mcfly
298211
298211
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
add a comment |
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
this should be done by array itself, isn't it?
– Faraz Naeem
Nov 11 at 15:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250043%2fhow-to-put-two-fields-result-set-in-to-array-list-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How second one is decided?
– user7294900
Nov 11 at 15:09
"QID" is the 1st field ,"Options" is the 2nd in query.I want "Options" field too in array list
– Faraz Naeem
Nov 11 at 15:12
rs.getString(2) ? Your question is quite vague Your query select QId, options and not names...
– mcfly
Nov 11 at 15:12
So you have to create an arrayList of a model with attributes QId and Options and fill that ArrayList with it.
– mcfly
Nov 11 at 15:15
@mcfly currently the arraylist is storing "QID" what want is QID = 1 , Options = 1 in arraylist example 1|1 , 2|1 , 3|2
– Faraz Naeem
Nov 11 at 15:21