零、前提
使用工具:idea、MySQL
使用技术:JDBC、servlet
一、数据库
Database - test1 # 数据库:test1
CREATE TABLE
student
(
id
int(3) NOT NULL auto_increment,
name
varchar(10) default NULL,
age
int(3) default NULL,
classes
varchar(10) default NULL,
PRIMARY KEY (id
),
UNIQUE KEYindex_1
(name
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; # 表:studentinsert into
student
(id
,name
,age
,classes
) values (7,'李让',28,'毕业'),(8,'张三',20,'大二'),(9,'张四',10,'三年级'),(11,'小张',10,'三年级'),(12,'张丹',17,'高二'),(13,'小暇',19,'大一'),(14,'小兰',19,'大一'),(15,'李杯狐',12,'五年级'); #表中的内容
二、项目框架
2-1 domain-Student 实体类
public class Student {
private int id;
private String name;
private int age;
private String classes;
public Student() {
}
public Student(String name, int age, String classes) {
this.name = name;
this.age = age;
this.classes = classes;
}
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 getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
}
2-2 util-JdbcUtils 工具类
public class JdbcUtils {
private final static String url = "jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8";
private final static String user = "root";
private final static String psw = "mysql";
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, psw);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
2-3 dao-StudentDao接口
public interface StudentDao {
public List<Student> findAll() throws SQLException, ClassNotFoundException;
public Student findById(int id) throws SQLException, ClassNotFoundException;
public int add(Student s) throws SQLException, ClassNotFoundException;
public int delete(int id) throws SQLException, ClassNotFoundException;
public int modify(int id ,Student s) throws SQLException, ClassNotFoundException;
}
2-4 dao-impl-StudentDaoImpl 接口实现
public class StudentDaoImpl implements StudentDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
@Override
public List<Student> findAll() throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "select * from student";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<Student> list = new ArrayList<Student>();
while(rs.next()){
Student s = new Student();
s.setId(rs.getInt("id"));
s.setAge(rs.getInt("age"));
s.setClasses(rs.getString("classes"));
s.setName(rs.getString("name"));
list.add(s);
}
return list;
}
@Override
public Student findById(int id) throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "select * from student where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
Student s = new Student();
while(rs.next()){
s.setId(rs.getInt("id"));
s.setAge(rs.getInt("age"));
s.setClasses(rs.getString("classes"));
s.setName(rs.getString("name"));
}
return s;
}
@Override
public int add(Student s) throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "insert into student(name,age,classes) values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,s.getName());
ps.setInt(2,s.getAge());
ps.setString(3,s.getClasses());
int i = ps.executeUpdate();
return i;
}
@Override
public int delete(int id) throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "delete from student where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
int i = ps.executeUpdate();
return i;
}
@Override
public int modify(int id, Student s) throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "update student set name=?,age=?,classes=? where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1,s.getName());
ps.setInt(2,s.getAge());
ps.setString(3,s.getClasses());
ps.setInt(4,id);
int i = ps.executeUpdate();
return i;
}
}
2-5 servle-StudentServlet
public class StudentServlet extends HttpServlet {
StudentDao studentDao = new StudentDaoImpl();
public List<Student> findAll() throws SQLException, ClassNotFoundException {
return studentDao.findAll();
}
public int modify(int id ,Student s) throws SQLException, ClassNotFoundException {
return studentDao.modify(id,s);
}
public Student findById(int id) throws SQLException, ClassNotFoundException {
return studentDao.findById(id);
}
public int add(Student s) throws SQLException, ClassNotFoundException{
return studentDao.add(s);
}
public int delete(int id) throws SQLException, ClassNotFoundException{
return studentDao.delete(id);
}
}
2-5 servle-StudentServletAdd
@WebServlet("/stu_add")
public class StudentServletAdd extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8"); //解决乱码
StudentServlet ss = new StudentServlet();
String name = req.getParameter("name");
String age = req.getParameter("age");
String classes = req.getParameter("classes");
Student s = new Student(name,Integer.parseInt(age),classes);
try {
ss.add(s);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
resp.sendRedirect("/five/stu_list.jsp");
}
}
2-6 web-stu_list.jsp
<%@ page import="cn.my.sms.servlet.StudentServlet" %>
<%@ page import="cn.my.sms.domain.Student" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生界面</title>
</head>
<body>
<h2 align="center">学生系统管理</h2>
<p align="center">欢迎你:XX <a href="login.html">安全退出</a></p>
<hr/>
<p align="center"><a href="stu_add.jsp">添加学生</a></p>
<form action="#" method="post">
<table align="center" border="1px" width="80%">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>班级</td>
<td>操作</td>
</tr>
<%
StudentServlet ss = new StudentServlet();
List<Student> list = ss.findAll();
for(Student s:list){
%>
<tr>
<td><%= s.getId() %></td>
<td><%= s.getName() %></td>
<td><%= s.getAge() %></td>
<td><%= s.getClasses() %></td>
<td>
<a href="stu_delete?id=<%= s.getId() %>" onclick="return confirm('真的要删除该学生吗?')">删除</a>|
<a href="stu_modify.jsp?id=<%= s.getId() %>">修改</a> </td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
2-6 web-stu_add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生界面</title>
</head>
<body>
<form action="stu_add" method="post">
<table align="center" border="1px">
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>班级</td>
<td><input type="text" name="classes"> </td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="添加学生"></td>
</tr>
</table>
</form>
</body>
</html>
2-7 servlet-StudentServletDelete
@WebServlet("/stu_delete")
public class StudentServletDelete extends HttpServlet {
StudentServlet ss = new StudentServlet();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
System.out.println("id = " + id);
int i = 0;
try {
i = ss.delete(Integer.parseInt(id));
} catch (Exception e) {
e.printStackTrace();
}
resp.sendRedirect("/five/stu_list.jsp");
}
}
2-8 servlet-StudentServletModify
@WebServlet("/stu_modify")
public class StudentServletModify extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8");
String id = req.getParameter("id");
String name = req.getParameter("name");
String age = req.getParameter("age");
String classes = req.getParameter("classes");
Student s = new Student(name,Integer.parseInt(age),classes);
StudentServlet ss = new StudentServlet();
try {
ss.modify(Integer.parseInt(id),s);
} catch (Exception throwables) {
throwables.printStackTrace();
}
resp.sendRedirect("stu_list.jsp");
}
}
2-9 web-stu_modify.jsp
<%@ page import="cn.my.sms.servlet.StudentServlet" %>
<%@ page import="cn.my.sms.domain.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改学生</title>
</head>
<body>
<form action="stu_modify" method="post">
<%
String id = request.getParameter("id");
StudentServlet ss = new StudentServlet();
Student s = ss.findById(Integer.parseInt(id));
%>
<input type="hidden" name="id" value="<%= s.getId() %>">
<table align="center" border="1px">
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="<%= s.getName() %>"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="<%= s.getAge() %>"></td>
</tr>
<tr>
<td>班级</td>
<td><input type="text" name="classes" value="<%= s.getClasses() %>"> </td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="修改学生"></td>
</tr>
</table>
</form>
</body>
</html>
三、总结
3-1 根据数据库创建实体类;
3-2 写工具类,获得 conn对象;
3-3 在StudentDao接口中写用到的方法,并在StudentDaoImpl类中实现所有的方法;
3-4 创建servlet包,在StudentServlet中调用所有的StudentDao接口中的方法(目的:方便管理、纠错、隔离);
3-5 创建主界面、新增学生界面、修改学生界面等三个页面;
3-6 在servlet包下创建类(StudentServletAdd、StudentServletDelete、StudentServletModify),三个类继承 HTTPServlet ,与三个页面结合实现新增、删除、修改等操作;
3-7 没有写登录页面。
版权归原作者 伴968 所有, 如有侵权,请联系我们删除。