java - 在JSP上使用超链接显示关系数据库记录的数据库

标签 java mysql jsp servlets jdbc

我基本上使用 Servlet 和 JDBC 在 JSP/JSTL 页面上显示 MySQL 数据库记录。我有两个表,一个是“员工表”,另一个是“位置表”(两个表都通过 PK 和 FK 相关)。现在我可以在 JSP 上显示员工表,如下所示:

Employee Table on JSP

现在,超链接出现在“部门#”列上,该列应该显示每个员工的“位置表”,而不是不同员工的所有行。我对此项目的编码包括用于 Employee & Location 表的 2 个 POJO 类、用于 JDBC 连接的 EmployeeDbUtil 类、Employee Servlet 以及用于显示 Employee & Location 表的两个 JSP 文件

Location Table for single Employee when clicked on hyperlink

现在我的问题是,我已经在 DbUtil 类上硬编码了 SQL 查询部分,以便通过说“where location.dept_no = 10”来获取位置表的单行,这就是为什么它给出相同的结果当单击任何超链接时,行,这不好。因此,请向我展示我可以使用的代码段,以便在单击不同员工的不同超链接时可以获得不同的结果。我真的不知道该怎么做,所以请在这里帮助我。谢谢!

这是我的完整代码(DbUtil、Servlet 和 JSP):

EMPLOYEE DbUtil(用于 JDBC 连接)

private DataSource dataSource;

    public EmployeeDbUtil(DataSource theDataSource) {

        dataSource = theDataSource;
    }

    public List<Employee> getEmployee() throws Exception {

        List<Employee> employees = new ArrayList<>();

        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;

        try {

            // Get connection

            myConn = dataSource.getConnection();

            // SQL Query

            String sql = "SELECT * FROM order_customer.employee; ";

            myStmt = myConn.createStatement();
            myRs = myStmt.executeQuery(sql);

            while (myRs.next()) {

                // retrieving data
                String employeeID = myRs.getString("employee_id");
                String name = myRs.getString("name");
                int salary = myRs.getInt("salary");
                String hireDate = myRs.getString("hire_date");
                int deptNum = myRs.getInt("dept_no");

                // create new customer object

                Employee tempEmployee = new Employee(employeeID, name, salary, hireDate, deptNum);

                // Now add tempCustomer to the ArrayList

                employees.add(tempEmployee);
            }

            return employees;
        }

        finally {
            close(myConn, myStmt, myRs);
        }
    }

    private void close(Connection myConn, Statement myStmt, ResultSet myRs) {

    }

    ////////////////////////////////////////////////////////////////////////

    public List<Location> getLocation() throws Exception {

        List<Location> location = new ArrayList<>();

        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;

        try {

            // Get connection

            myConn = dataSource.getConnection();

            // SQL Query

            String sql = "SELECT location.dept_no,location.state,location.city \r\n" + 
                    "from order_customer.employee JOIN order_customer.location\r\n" + 
                    "on employee.dept_no=location.dept_no\r\n" + 
                    "WHERE location.dept_no = '10' ";

            myStmt = myConn.createStatement();
            myRs = myStmt.executeQuery(sql);

            while (myRs.next()) {

                // retrieving data

                int deptNum = myRs.getInt("dept_no");
                String state = myRs.getString("state");
                String city = myRs.getString("city");

                // create new customer object

                Location tempLocation = new Location(deptNum, state, city);

                location.add(tempLocation);
            }

            return location;
        }

        finally {
            close(myConn, myStmt, myRs);
        }
    }
}

Servlet 代码:

 private EmployeeDbUtil employeeDbUtil;

    @Resource(name = "jdbc/order_customer")
    private DataSource dataSource;

    @Override
    public void init() throws ServletException {
        super.init();

        try {
            employeeDbUtil = new EmployeeDbUtil(dataSource);
        } catch (Exception exc) {
            throw new ServletException(exc);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            // read the "command" parameter
            String theCommand = request.getParameter("command");

            if (theCommand == null) {
                theCommand = "LIST";
            }

            // route to the appropriate method
            switch (theCommand) {

            case "LIST":
                listEmployee(request, response);
                break;

            case "LOAD":
                loadLocation(request, response);
                break;

            default:
                listEmployee(request, response);
            }

        } catch (Exception exc) {

            throw new ServletException(exc);
        }
    }

    private void loadLocation(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<Location> location = employeeDbUtil.getLocation();

        request.setAttribute("THE_LOCATION", location);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/location-info.jsp");
        dispatcher.forward(request, response);

    }

    private void listEmployee(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<Employee> employees = employeeDbUtil.getEmployee();

        request.setAttribute("EMPLOYEE_LIST", employees);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/list-employee.jsp");
        dispatcher.forward(request, response);
    }
}

JSP页面代码:

<
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>

<html>
<head>
<title>Employee Details</title>
<link type="text/css" rel="stylesheet" href="css/style.css">

</head>

<body>
    <div id="wrapper">
        <div id="header">
        </div>
    </div>

    <div id="container">
        <div id="content">

            <table>

                <tr>

                    <th>Employee ID</th>
                    <th>Name</th>
                    <th>Salary</th>
                    <th>Hire Date</th>
                    <th>Department #</th>

                </tr>

                <c:forEach var="tempEmployee" items="${EMPLOYEE_LIST}">

                    <c:url var="employeeLink" value="EmployeeServlet">
                        <c:param name="command" value="LOAD" />
                        <c:param name="deptNum" value="${tempEmployee.deptNum}" />
                    </c:url>

                    <tr>
                        <td>${tempEmployee.employeeID}</td>
                        <td>${tempEmployee.name}</td>
                        <td>${tempEmployee.salary}</td>
                        <td>${tempEmployee.hireDate}</td>
                        <td> <a href= "${employeeLink}"> ${tempEmployee.deptNum} </a></td>
                </c:forEach>

            </table>

        </div>

    </div>
</body>

位置表 JSP:

<<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

<head>

<title>Employee Department</title>

<link type="text/css" rel="stylesheet" href="css/style.css">

</head>

<body>

    <div id="wrapper">
        <div id="header">
            <h2>Department Details</h2>
        </div>
    </div>

    <div id="container">

        <div id="content">

            <table>

                <tr>
                    <th>Department #</th>
                    <th>State</th>
                    <th>City</th>

                </tr>

                <c:forEach var="tempLocation" items="${THE_LOCATION}">

                    <tr>
                        <td>${tempLocation.deptNum}</td>
                        <td>${tempLocation.state}</td>
                        <td>${tempLocation.city}</td>
                        <%--    <td> ${tempLocation.name} </td> --%>

                    </tr>
                </c:forEach>

            </table>

        </div>

    </div>

</body>
</html>

最佳答案

您可以通过与“command”相同的方式获取“deptNum”参数,将其传递给DBUtil,然后替换“10”

关于java - 在JSP上使用超链接显示关系数据库记录的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49483219/

相关文章:

mysql - 无法从另一个容器访问Docker容器中的MySQL数据库

java - 如果从 jar 运行,带有嵌入式 jetty 的 Spring 应用程序找不到 webdefault.xml

java - Spring MVC JSP Jquery 在按钮单击后重定向错误上调用 Controller 方法

java - 用于导入 Excel 工作表然后将其存储到 servlet 中的数据库中的省时程序

java - Cordova 4.1.2 CLI Android 构建失败 Windows

sql - mysql:导出单行,但具有所有依赖项

php - 收到错误消息,由于错误,我的图像无法显示

java - Jdownloader 源代码和结构文档

java - 使用 Java 通过 SSL 的 LDAP

java - Weka 安装 libsvm