, . - , . :
:
1. UserID -> Type: Int
2. User_Name -> Type: Varchar
3. User_Contact -> Type: Varchar
...
Now you can simply write RowMapper to map all of these fields to your own POJO object, for example:
POJO Class:
public class User{
private int userId;
private String userName;
private String userContact;
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
/* Other Getter-Setter(s) */
}
Query String:
private static final String SELECT_QUERY = "SELECT * FROM user WHERE id = ?";
JdbcTemplate Call: Here you go userIdfor ?. JdbcTemplate will take care of this automatically.
List<User> = (List<User>) jdbcTemplate.query(SELECT_QUERY, new Object[] { userId }, new UserRowMapper());
Finally, the UserRowMapper class:
class UserRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet resultSet, int row) throws SQLException {
User user = new User();
user.setUserId(resultSet.getInt("UserID"));
user.setUserName(resultSet.getString("User_Name"));
user.setUserContact(resultSet.getString("User_Contact"));
return user;
}
}
This is really the best and recommended way to use JdbcTemplate.
source
share