ListArray Saving Identical Entries

I am trying to save a database table in a ListArray of the Students class .

public List<Students> getData() {
    //_Students.clear();
    Students tempStudent = new Students();
    List<Students> students = new ArrayList<>();
    dbConnect();
    try {
        stmt = c.createStatement();
        rs = stmt.executeQuery("SELECT * FROM Students;");
        int size = 0;
        while (rs.next()) {
            tempStudent.studentId = rs.getInt("StudentNo");
            tempStudent.studentName = rs.getString("StudentName");
            tempStudent.studentAge = rs.getInt("StudentAge");
            students.add(tempStudent);
            size++;
        }
        rs.close();
        c.commit();
        stmt.close();
        c.close();
    } catch (SQLException e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    return students;
}

In a while loop, when I try to print the data as

   System.out.println("Student Id: " + tempStudent.studentId);

it is perfectly imprinted. But when I try to print it like

for (int i = 0; i < size; i++) {
        System.out.println("Student Id: " + student.get(i).studentId);
    }

It prints the last record that was read from the database. The number of entries is the same. If 4 rows are stored in the database table, the displayed record will also be 4 times.

Is there something wrong with the way I use LIST? Thank!

+4
source share
6 answers
Students tempStudent = new Students(); 

change it to

Students tempStudent;

overridding property of same object, tempStudent outside while loop. objects, number of record . tempStudent object, .

while (rs.next()) {
        tempStudent = new Students();
        tempStudent.studentId = rs.getInt("StudentNo");
        tempStudent.studentName = rs.getString("StudentName");
        tempStudent.studentAge = rs.getInt("StudentAge");
        students.add(tempStudent);
        size++;
    }
+4

, .

tempStudent = new Students();
tempStudent.studentId = rs.getInt("StudentNo");
tempStudent.studentName = rs.getString("StudentName");
tempStudent.studentAge = rs.getInt("StudentAge");
students.add(tempStudent);
size++;
+4

Student while,

while (rs.next()) {

            tempStudent = new Students();
            tempStudent.studentId = rs.getInt("StudentNo");
            tempStudent.studentName = rs.getString("StudentName");
            tempStudent.studentAge = rs.getInt("StudentAge");
            students.add(tempStudent);
            size++;
        }

​​ ​​ , - , .

+2

tempStudent. , ArrayList -. ​​.

+1

, :

  public class Students {

    private int StudentId = 0;
    private String StudentName = "defaultName";
    private int StudentAge = 18;

    public int getStudentId() {
        return StudentId;
    }
    public void setStudentId(int studentId) {
        StudentId = studentId;
    }
    public String getStudentName() {
        return StudentName;
    }
    public void setStudentName(String studentName) {
        StudentName = studentName;
    }
    public int getStudentAge() {
        return StudentAge;
    }
    public void setStudentAge(int studentAge) {
        StudentAge = studentAge;
    }
}

, - !

+1

:

public List<User> getAllUsers()
    {
        List<User> users=new ArrayList<User>();
    try
    {
            Statement stmt=connection.createStatement();
            ResultSet rs=statement.executeQuery("Select * from Students");

            while(rs.next())
            {
                User user=new User();
                user.setUserid(rs.getInt(("StudentNo");));
                user.setFirstName(rs.getString("StudentName"));
                user.setLastName(rs.getString("StudentAge"));
                users.add(user);
            }
     }
    catch(SQLException e)
        {
            e.printStackTrace();
        }
            return users;
    }

. :

   A.jsp> conntroller.java > dao.java>dbUtil.java 

:

jsp - ** (A.jsp) . , ** , , - , java-. java- (EX. Update). ( ).

A.jsp( )

First Name: <input type="text" name="firstName" 
            value="<c:out value="${user.firstName}"/>"/><br/>

Controller.java

public UserController() 
{
dao = new UserDao();
}
{
//controller operation like update;
}

Dao.java

public class UserDao 
{
private Connection connection;
public UserDao()
{
connection=DbUtil.getConnection();
}
public void updateUser(User user)
{
//update operation, and here only using the user 
}

DbUtil.java

public class DbUtil 
{
public static Connection con = null;
public static Connection getConnection()
{ 
//db connection 
}
}
+1

Source: https://habr.com/ru/post/1529739/


All Articles