<!-- jsp页面 -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.unit5.service.EmpService"%>
<%@ page import="com.unit5.utils.Condition"%>
<%@ page import="com.unit5.pojo.Emp"%>
<%@ page import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
EmpService es=new EmpService();
//获取HTML条件参数
String idCondition=request.getParameter("id");
String nameCondition=request.getParameter("name");
String deptNameCondition=request.getParameter("deptName");
//构造条件对象
Condition cd=new Condition(idCondition,nameCondition,deptNameCondition);
//获取条件查询结构
ArrayList<Emp> empsList=es.findEmps(cd);
//将获取数据保存在作用域中
request.setAttribute("empList", empsList);
%>
<!-- 头部 -->
<div style="background-color:cornflowerblue; color:white; width:100%; height:150px;">
<h1 style="line-height:80px; margin-left:30px;">人力资源管理系统</h1>
</div>
<!-- 菜单 -->
<table style="border:1px solid cornflowerblue; width:100%;">
<tr>
<td width="10%"><a href="http://127.0.0.1:8080/unit5/emp_index.jsp">首页</a></td>
<td width="10%"><a href="http://127.0.0.1:8080/unit5">添加</a></td>
<td width="10%"><a href="http://127.0.0.1:8080/unit5">其他</a></td>
<td></td>
<td></td>
<td width="50%"></td>
</tr>
</table>
<!-- 条件查询 -->
<br/><form action="http://127.0.0.1:8080/unit5/emp_index。jsp" method="get" style="width:100%">
查询条件:员工id<input type="number" style="width:100px;" name="id">
姓名<input type="text" style="width:100px;" name="name">
部门<select style="width:160px; height:30px;" name="deptName">
<option value="所有">所有</option>
<option value="研发部">研发部</option>
<option value="行政部">行政部</option>
<option value="销售部">销售部</option>
<option value="董事会">董事会</option>
<option value="财务部">财务部</option>
</select>
<input type="submit" value="查询数据">
<input type="reset" value="清空">
</form><br/>
<!-- 数据查询 -->
<table border="1px" style="width:100%">
<tr>
<td>员工id</td>
<td>姓名</td>
<td>年龄</td>
<td>职位</td>
<td>入职日期</td>
<td>薪资</td>
<td>部门</td>
<td>操作</td>
</tr>
<c:forEach items="${requestScope.empList}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.age}</td>
<td>${emp.job}</td>
<td>${emp.hireDate}</td>
<td>${emp.salary}</td>
<td>${emp.deptName}</td>
<td><a href="连接到删除功能jsp">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
//实体类构造方法
package com.unit5.pojo;
import java.sql.Date;
public class Emp {
private int id;
private String name;
private int age;
private String job;
private Date hireDate; //日期要用Date类进行定义,在此处要用中Date
private double salary;
private String deptName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
//具体业务类
package com.unit5.service;
import java.sql.SQLException;
import java.util.ArrayList;
import com.unit5.DAO.EmpDAO;
import com.unit5.pojo.Emp;
import com.unit5.utils.Condition;
public class EmpService {
//条件查询数据
EmpDAO ed=new EmpDAO();
public ArrayList findEmps(Condition cd) throws SQLException {
ArrayList<Emp> empList=new ArrayList<Emp>();
String sql="select * from emps where 1=1"; //1=1默认查询所有
System.out.println("查询条件:"+cd.id+"-"+cd.name+"-"+cd.deptName);
if(cd.id!=null && cd.id!="") {
sql=sql+" and id="+cd.id; //字段拼接,注意空格;
}
if(cd.name!=null && cd.name!="") {
sql=sql+" and name='"+cd.name+"'"; //字符串要加单引号,java中字符串要加双引号 ,在此程式中单引号要被双引号阔上
}
if(cd.deptName!=null && cd.deptName!="") {
sql=sql+" and deptName='"+cd.deptName+"'";
}
System.out.println("条件查询sql:"+sql);
empList=ed.findEmpBySql(sql);
return empList;
}
}
//数据库连接
package com.unit5.utils;
import java.sql.Connection;
import java.sql.DriverManager;
public class GetConnection {
private static String address="jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimeZone=UTC";
private static String dataBaseName="";
private static String dataBasePwd="";
public static Connection getConn(){
Connection conn=null;
try{
//加载jdbc驱动,固定程式
Class.forName("com.mysql.cj.jdbc.Driver");
//链接数据库,固定程式
conn=DriverManager.getConnection(address,dataBaseName,dataBasePwd);
}catch(Exception e){
e.printStackTrace();
System.out.println("数据库链接失败!");
}
return conn;
}
}
//条件查询数据实体类构造方法
package com.unit5.utils;
public class Condition {
public String id;
public String name;
public String deptName;
public Condition(String id, String name, String deptName) {
this.id = id;
this.name = name;
this.deptName = deptName;
}
}
//增、删、改、查的公共方法
package com.unit5.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.unit5.pojo.Emp;
import com.unit5.utils.GetConnection;
public class EmpDAO {
//查询数据公共方法
public ArrayList findEmpBySql(String sql) throws SQLException {
ArrayList<Emp> empList=new ArrayList<Emp>(); //特指Emp类里的参数
Connection conn=GetConnection.getConn();
Statement stmt=conn.createStatement(); //jdbc数据库连接固定程式
ResultSet rs=stmt.executeQuery(sql); //将外部传进的sql传进执行,有返回值的用executeQuery,直接执行的用execute;
while(rs.next()) { //循环保存sql的数据,每循环一次累加保存到empList里,直到保存完毕为止;
Emp emp=new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setAge(rs.getInt("age"));
emp.setJob(rs.getString("job"));
emp.setHireDate(rs.getDate("hireDate"));
emp.setSalary(rs.getDouble("salary"));
emp.setDeptName(rs.getString("deptName"));
empList.add(emp);
}
conn.close(); //关闭连接
stmt.close(); //关闭连接
return empList;
}
}
执行后就变成这样,求大神解答?