引入JavaBean的目的是把一部分java代码整合起来,编译重用,不糅合到jsp里面。
首先新建一个web工程,idea结构图如下:
添加依赖
pom.xml
新增依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
工程包括list.jsp、add.jsp、add_do.jsp、StaffDao.java
等几个文件组成
com.critc.staff.StaffDao,主要是连库、查询列表、新增员工等java方法
package com.critc.staff;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 孔垂云
*/
public class StaffDao {
/**
* 获取数据库连接
* @return
*/
public Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");//指定连接类型
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "root", "root");//获取连接
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 员工列表
*
* @return
*/
public List<Staff> list() {
List<Staff> list = new ArrayList<>();
Connection conn = getConn();
try {
PreparedStatement pstmt = conn.prepareStatement("select * from staff");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Staff staff = new Staff();
staff.setId(rs.getInt("id"));
staff.setName(rs.getString("name"));
list.add(staff);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 新增
*
* @param name
*/
public void add(String name) {
Connection conn = getConn();
try {
PreparedStatement pstmt = conn.prepareStatement("insert into staff(name) values(?)");
pstmt.setString(1, name);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
list.jsp 员工列表页面
<%@page import="com.critc.staff.Staff"%>
<%@page import="java.util.List"%>
<%@page import="com.critc.staff.StaffDao"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
员工列表
<a href="add.jsp"> 新增</a>
<br />
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
</tr>
<%
StaffDao staffDao = new StaffDao();
List<Staff> list = staffDao.list();
for (Staff staff : list) {
%>
<tr>
<td><%=staff.getId()%></td>
<td><%=staff.getName()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
add.jsp 新增页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
新增员工
<form action="add_do.jsp" method="post">
姓名:<input type="text" name="name"><br> <input
type="submit" value="保存">
</form>
</body>
</html>
add_do.jsp 执行新增页面
<%@page import="com.critc.staff.StaffDao"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
StaffDao staffDao = new StaffDao();
staffDao.add(request.getParameter("name"));
response.sendRedirect("list.jsp");
%>
</body>
</html>