Results 1 to 2 of 2
- 07-21-2012, 07:07 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
How do I display arrayList contents from resultset in Java JDBC?
So I am making a simple java project to play around with JDBC in glassfish and see how it works. The program just shows you a list of surveys and a list of questions for the survey you select. However i cant seem to display the list of questions for the survey I selected. I keep getting empty values. These are the methods I have created:
**convert the resultset to object model data values**
Java Code:public JHAKSurvey findSurvey(long id) { System.out.println("JDBC: FIND SURVEY"); Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; JHAKSurvey survey = null; try { connection = openConnection(); String query = "SELECT * FROM APP.SURVEY WHERE ID=?"; ps = connection.prepareStatement(query); ps.setLong(1, id); rs = ps.executeQuery(); while (rs.next()) { survey = createSurveyFromResultSet(rs); } } catch (Exception e) { e.printStackTrace(); } finally { closeConnection(connection); } return survey; }
**private method to query the list of questions from the QUESTION table for a survey id**
**private method to convert the find the resultset list to an question object and add it to the survey object**Java Code:private void findQuestionsBySurvey(JHAKSurvey survey){ System.out.println("JDBC: FIND QUESTIONS BY SURVEY"); Connection connection = null; PreparedStatement ps = null; try { connection = openConnection(); String query = "SELECT * FROM APP.QUESTION WHERE SURVEYID=?"; ps = connection.prepareStatement(query); ps.setLong(1, survey.getId()); ps.executeQuery(query); } catch (Exception e) { e.printStackTrace(); } finally { closeConnection(connection); } }
**private method to convert a resultset to an survey object.**Java Code:private void createQuestionFromResultSet(ResultSet rs, JHAKSurvey survey){ ArrayList<JHAKQuestion> qList = new ArrayList<JHAKQuestion>(); JHAKQuestion question = new JHAKQuestion(); JHAKSurvey ss = new JHAKSurvey(); //qList.add(survey.getQuestions()); try { while (rs.next()) { //question.setDescription(qList.toString()); question.setId(rs.getLong("ID")); question.setDescription(rs.getString("DESCRIPTION")); qList.add(question); survey.setQuestions(qList); } System.out.println("createQuestionFromResultSet : JDBC : successful"); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("createQuestionFromResultSet : JDBC : fail"); e.printStackTrace(); } }
What am I missing? I also seem to get error:Java Code:private JHAKSurvey createSurveyFromResultSet(ResultSet rs){ JHAKSurvey survey = new JHAKSurvey(); Boolean active = false; String yes; try { yes = rs.getString("ACTIVE"); survey.setId(rs.getLong("ID")); survey.setTitle(rs.getString("TITLE")); if (yes.equals(Character.toString('Y'))) { survey.setActive(true); } else { survey.setActive(false); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return survey; }
cannot convert from void to JHAKQuestion
When I try the method: createQuestionFromResultSet();
Thank You
- 07-23-2012, 10:37 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I display arrayList contents from resultset in Java JDBC?
I don't see anything that shows how these things fit together.
I will say, using a 'while (rs.next())' when you are only expecting a single result is confusing.
That should be 'if (rs.next())', otherwise it looks like you are expecting more than one and therefore the fact you are only using a single survey would imply a bug.
You don't need to add the 'qlist' each time round the loop.
Either set it before entering the loop (my preference) or after the loop.
It's the same reference, so anything done to 'qlist' in that method affects the same object that the reference held in the 'survey' is pointing to.
Oh yes, shouldn't findQUestionsBySurvey be creating JHAKQuestion objects?
You run the query and do nothing with the result.Please do not ask for code as refusal often offends.
Similar Threads
-
jdbc resultset to xml
By shr2413 in forum XMLReplies: 2Last Post: 03-30-2011, 04:39 AM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
Display only certain contents of text file and edit display
By blkshrk81 in forum New To JavaReplies: 1Last Post: 12-01-2010, 06:35 PM -
add to arrayList in resultSet
By mayub in forum Advanced JavaReplies: 6Last Post: 04-29-2009, 03:14 AM -
resultSet jdbc getObject( ? )
By Peter in forum JDBCReplies: 2Last Post: 07-04-2007, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks