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










share|improve this question




















  • 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















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










share|improve this question




















  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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






share|improve this answer





















  • 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


















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)




  1. you create a class with all attributes (id, option, name...), let
    call this POJO

  2. query your fields

  3. iterate over each result and
    create your POJO Object

  4. add it to the list


there is plenty of other thing you could do here but this is the beginning of a database mapping






share|improve this answer





















  • this should be done by array itself, isn't it?
    – Faraz Naeem
    Nov 11 at 15:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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















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






share|improve this answer





















  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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












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)




  1. you create a class with all attributes (id, option, name...), let
    call this POJO

  2. query your fields

  3. iterate over each result and
    create your POJO Object

  4. add it to the list


there is plenty of other thing you could do here but this is the beginning of a database mapping






share|improve this answer





















  • this should be done by array itself, isn't it?
    – Faraz Naeem
    Nov 11 at 15:47















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)




  1. you create a class with all attributes (id, option, name...), let
    call this POJO

  2. query your fields

  3. iterate over each result and
    create your POJO Object

  4. add it to the list


there is plenty of other thing you could do here but this is the beginning of a database mapping






share|improve this answer





















  • this should be done by array itself, isn't it?
    – Faraz Naeem
    Nov 11 at 15:47













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)




  1. you create a class with all attributes (id, option, name...), let
    call this POJO

  2. query your fields

  3. iterate over each result and
    create your POJO Object

  4. add it to the list


there is plenty of other thing you could do here but this is the beginning of a database mapping






share|improve this answer












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)




  1. you create a class with all attributes (id, option, name...), let
    call this POJO

  2. query your fields

  3. iterate over each result and
    create your POJO Object

  4. add it to the list


there is plenty of other thing you could do here but this is the beginning of a database mapping







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly