mercredi 13 juin 2018

How to retrive values from database using layered architecture java

Here I'm developing java web application for my mid semester project.I'm using layered architecture and I want to retrieve values from database and display it on a web browser.I tried so hard but there's something wrong with my code.

here's my code

AdminDaoImpl Layer

@Override
public List<UserModel> retrive() throws SQLException {

    String sql = "SELECT USERID, NAME, MOBILE FROM USER";
    List<UserModel> read = null;
    try {
        Statement statement = connection.getConnection().createStatement();
        ResultSet rst = statement.executeQuery(sql);

        while (rst.next()) {
            if (null == read) {
                read = new ArrayList<UserModel>();
            }

            UserModel userModel = new UserModel(rst.getString("USERID"), rst.getString("NAME"), rst.getString("MOBILE"));
            read.add(userModel);
        }

    } catch (SQLException ex) {
        Logger.getLogger(AdminDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        throw ex;
    }
    return read;
}

AdminServiceImpl Layer

@Override
public List<UserDto> read() {

    List<UserDto> readAll = new ArrayList<>();
    try {
        List<UserModel> getAll = adminDao.retrive();
        for (UserModel userModel : getAll) {
            UserDto userDto = new UserDto(userModel.getUserId(), userModel.getName(), userModel.getMobile());
            readAll.add(userDto);
        }
    } catch (SQLException ex) {
        Logger.getLogger(AdminServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return readAll;
}

AdminController Layer

I need to implement this layer but I have no idea how to do it

can someone one help me with this? Thanks in Advanced.

Aucun commentaire:

Enregistrer un commentaire