Results 1 to 2 of 2
  1. #1
    n3xtgen is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default 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**
    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 the find the resultset list to an question object and add it to the 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();
    		}
    	}
    **private method to convert a resultset to an survey object.**

    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;
    	}
    What am I missing? I also seem to get error:

    cannot convert from void to JHAKQuestion
    When I try the method: createQuestionFromResultSet();

    Thank You

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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.

    ** This space for rent **

Similar Threads

  1. jdbc resultset to xml
    By shr2413 in forum XML
    Replies: 2
    Last Post: 03-30-2011, 04:39 AM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  4. add to arrayList in resultSet
    By mayub in forum Advanced Java
    Replies: 6
    Last Post: 04-29-2009, 03:14 AM
  5. resultSet jdbc getObject( ? )
    By Peter in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •