我基本上是使用Servlet& amp ;;在JSP / JSTL页面上显示MySQL数据库记录。 JDBC。我有两个表,一个是"员工表" &安培;其他"位置表"(两个表都通过PK& FK相关)。现在,我可以在JSP上显示Employee Table,如下所示:
现在超链接出现在列#34;部门#"应该显示"位置表"对于每个员工,而不是所有不同员工的行。我为这个项目编写的代码包括 2个POJO类,用于员工和位置表,用于JDBC连接的EmployeeDbUtil类,Employee Servlet和Employee&amp ;;的两个JSP文件。位置表显示。
Location Table for single Employee when clicked on hyperlink
现在我的问题是我在DbUtil类上硬编码了SQL查询部分,通过说"来获取位置表的单行;其中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 PAGE CODE:
<
<%@ 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>
答案 0 :(得分:0)
你可以得到&#34; deptNum&#34;参数与&#34;命令&#34;相同,将其传递给DBUtil,然后替换&#39; 10&#39;