A
A
Andrey Romanov2017-10-05 15:56:31
Java
Andrey Romanov, 2017-10-05 15:56:31

How to retrieve the required information from the database?

Hello!

There is a database in which tables "Profile" and "Users".

There is a servlet ShowUsersController that pulls out information about all users.

pom.xml :
https://pastebin.com/p2qyf6q3

ShowUsersController.java :

package com.romanidze.jizzyshop.controllers;

import com.romanidze.jizzyshop.dao.implementations.ProfileDAOImpl;
import com.romanidze.jizzyshop.dao.interfaces.ProfileDAOInterface;
import com.romanidze.jizzyshop.models.Profile;
import com.romanidze.jizzyshop.utils.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet(
        name = "ShowUsersController",
        description = "Пользователи сервиса",
        urlPatterns = {"/admin/users"}
)
public class ShowUsersController extends HttpServlet{

    private Connection conn;

    @Override
    public void init(){

        try{

            Class.forName("org.postgresql.Driver");

            DBConnection dbConnection = new DBConnection(getServletContext()
                                                            .getResourceAsStream("/WEB-INF/properties/db.properties"));

            Map<String, String> configMap = new LinkedHashMap<>();
            configMap.putAll(dbConnection.getDBConfig());

            this.conn = DriverManager.getConnection(configMap.get("db_url"), configMap.get("db_username"),
                                                    configMap.get("db_password"));

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){

        try {

            ProfileDAOInterface profileDAO = new ProfileDAOImpl(this.conn);

            List<Profile> profiles = profileDAO.findAll();

            req.setAttribute("profiles", profiles);

            req.getRequestDispatcher("/WEB-INF/views/show_users.jsp").forward(req, resp);

        }catch(ServletException | IOException e){

            e.printStackTrace();

        }finally{
            try {
                this.conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}


show_users.jsp: https://pastebin.com/Yuk8pLZv

ProfileDAOImpl.java:
https://pastebin.com/cbW8jM3E

Profile.java:
package com.romanidze.jizzyshop.models;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;

@Getter
@Setter
@AllArgsConstructor
@Builder
@EqualsAndHashCode
public class Profile {

    private Long id;
    private String country;
    private String gender;
    private String subscription;
    private Long userId;

    @Override
    public String toString(){

        StringBuilder sb = new StringBuilder();

        sb.append("Profile{")
          .append("id = ").append(this.getId())
          .append(", country = ").append(this.getCountry())
          .append(", gender = ").append(this.getGender())
          .append(", subscripttion = ").append(this.getSubscription())
          .append(", userID = ").append(this.getUserId());

        return sb.toString();

    }

}


However, JSTL templates do not output any information.
The DBConnection class contains only a connection to the database, and nothing more.
How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Romanov, 2017-10-10
@Romanidze

Decided by switching to Thymeleaf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question