0

I have a class that has two lists of Strings.

public class School {
private List<String> students;
private List<String> teachers;
//getters and setters
}

I have the below method in my DAO class where the school object is getting populated.

public static School fetchSchoolInfo(String schoolName) throws SQLException
{       
    School school = null;
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    List<String> students = new ArrayList<String>();
    List<String> teachers = new ArrayList<String>();        
    try {
        connection = createConnection();
        statement = connection.prepareStatement("SELECT COL_STUDENT,COL_TEACHERS FROM SCHOOL WHERE SCHOOLNAME=?");
        statement.setString(1, schoolName);
        rs = statement.executeQuery();
        while(rs.next())
        {
            school = new School();                              
            students.add(rs.getString(1));
            teachers.add(rs.getString(1));
            school.setStudents(students);
            school.setTeachers(teachers);               
        }                       
    } finally{
        rs.close();
        statement.close();
        connection.close();
    }

    return school;
}

Now, if I do this,

School school = MyDAO.fetchPersonDeviceIdInfo("SJSU");
System.out.println(school);

Will the school object have all the info of SJSU? I know I can ideally try this on my own and check, but my application is very complex, and it will take a while to integrate this code with my application. Please suggest. Also, do I need a to string?

Many thanks!

5
  • 1
    COL_TEACHERS is second column, so you need teachers.add(rs.getString(2));. school = new School() should be before the while loop,and school.setStudents() and school.setTeachers() should be after the while loop. You might also want to check if your connection, statement, result set objects are null before trying to close them. Commented Oct 22, 2013 at 5:38
  • teachers.add(rs.getString(1)); should be teachers.add(rs.getString(2)); Commented Oct 22, 2013 at 5:38
  • The answer actually depend on the table structure. Please post those details. Commented Oct 22, 2013 at 5:39
  • Sorry, I typed my code on this page. So I got it wrong. I actually have it as 2 :) Commented Oct 22, 2013 at 5:40
  • @Thihara - The table has School information. It has multiple columns that I don't feel comfortable posting here. The columns include teachers, students, teacher address, student contact information etc. What I am trying to find it, for each school, what are the list of students, and teachers. Does that help? Commented Oct 22, 2013 at 5:42

2 Answers 2

5

change the following :

while(rs.next())
    {
        school = new School();                              
        students.add(rs.getString(1));
        teachers.add(rs.getString(1));
        school.setStudents(students);
        school.setTeachers(teachers);               
    }  

to

school = new School(); 
while(rs.next())
    {

        students.add(rs.getString(1));
        teachers.add(rs.getString(2)); // changed

    }  

school.setStudents(students);
school.setTeachers(teachers);  
Sign up to request clarification or add additional context in comments.

6 Comments

Change teachers.add(rs.getString(1)); to teachers.add(rs.getString(2));
Sure. What is the difference between instantiating the object in the while loop vs outside. I've seen a few examples online where they instantiate inside so for each row in the resultset, a new object gets created so the object has all the records, and not just the last ones. Can you please explain the difference?
when u instantiate it inside the loop say u have four records, each time a new school obj will be created, and the last one will be overwritten @rickygrimes
if it were a list of school objects then in that case, instantiating school inside wud have worked, where at the end you could add school to the list. :)..
Okay guys, thank you so much. You guys are awesome :) I hope to become like one of you :)
|
1

Override your School.toString() method like this

class School {
  private List<String> students;
  private List<String> teachers;
  @Override
  public String toString() {
      return "School [students=" + students + ", teachers=" + teachers + "]";
  } 
}

Then you try

School school = MyDAO.fetchPersonDeviceIdInfo("SJSU");
System.out.println(school);

1 Comment

Sure. Thank you ByteCode. I just wanted to make sure if overriding is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.