Mapping a JDBC ResultSet to an object

前端 未结 8 1246
生来不讨喜
生来不讨喜 2020-12-04 18:14

I have a user class that has 16 attributes, things such as firstname, lastname, dob, username, password etc... These are all stored in a MySQL database and when I want to re

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 18:28

    No need of storing resultSet values into String and again setting into POJO class. Instead set at the time you are retrieving.

    Or best way switch to ORM tools like hibernate instead of JDBC which maps your POJO object direct to database.

    But as of now use this:

    List users=new ArrayList();
    
    while(rs.next()) {
       User user = new User();      
       user.setUserId(rs.getString("UserId"));
       user.setFName(rs.getString("FirstName"));
      ...
      ...
      ...
    
    
      users.add(user);
    } 
    

提交回复
热议问题