数据库先创建一张商品表
创建工程
实体类
public class Employee {
private int id;
private String name;
private int age;
private double salary;
private String sex;
public Employee() {
}
public Employee(int id, String name, int age, double salary, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.sex = sex;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", sex='" + sex + '\'' +
'}';
}
}
工具类
public class DBUtil {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection("jdbc:mysql:///test?characterEncodingUTF-8","root","root");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static boolean executeupdate(String sql,Object... args){
Connection conn=null;
PreparedStatement ps=null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
int i = ps.executeUpdate();
return i>0;
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(null,ps,conn);
}
return false;
}
public static List<Map<String,Object>> executequery(String sql,Object... args){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for(int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
rs = ps.executeQuery();
int count = rs.getMetaData().getColumnCount();
List<Map<String,Object>> list = new ArrayList<>();
while (rs.next()){
Map<String, Object> map = new HashMap<>();
for(int i=0;i<count;i++){
String label = rs.getMetaData().getColumnLabel(i + 1);
map.put(label,rs.getObject(label));
}
list.add(map);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(rs,ps,conn);
}
return null;
}
public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
接口
public interface IEmployeeDAO {
List<Map<String,Object>> getAllEmployee();
}
实现类
public class EmployeeDAOImpl implements IEmployeeDAO {
//查询所有
@Override
public List<Map<String, Object>> getAllEmployee() {
String sql="select * from employee";
return DBUtil.executequery(sql);
}
}
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>/EmployeeServlet</servlet-name>
<servlet-class>com.chang.servlet.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>/EmployeeServlet</servlet-name>
<url-pattern>/emp</url-pattern>
</servlet-mapping>
</web-app>
创建servlet
public class EmployeeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据库中的数据
IEmployeeDAO dao=new EmployeeDAOImpl();
List<Map<String, Object>> list = dao.getAllEmployee();
//请求共享数据
req.setAttribute("date",list);
//请求转发到某个jsp
req.getRequestDispatcher("Employee.jsp").forward(req,resp);
}
}
创建JSP文件
<html>
<head>
<title>员工首页</title>
</head>
<body>
//获取共享数据 用java代码获取共享的数据 共享数据的时候用request 需要手动强转类型
<%List<Map<String,Object>>list= (List<Map<String, Object>>) request.getAttribute("date");%>
<table border="1px" cellspacing="0" width="300px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>薪水</th>
<th>性别</th>
</tr>
<%for(Map map:list){%>
<tr>
<td><%=map.get("id")%></td>
<td><%=map.get("name")%></td>
<td><%=map.get("age")%></td>
<td><%=map.get("salary")%></td>
<td><%=map.get("sex")%></td>
</tr>
<%}%>
</table>
</body>
</html>
最后页面显示