基于Java Web的学生管理系统
零、前提
使用工具: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 KEY `index_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 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 findAll() throws SQLException, ClassNotFoundException {
conn= JdbcUtils.getConn();
String sql = "select * from student";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List list = new ArrayList();
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 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
学生界面
学生系统管理
欢迎你:XX 安全退出
添加学生
学号
姓名
年龄
班级
操作
<%
StudentServlet ss = new StudentServlet();
List list = ss.findAll();
for(Student s:list){
%>
" onclick="return confirm('真的要删除该学生吗?')">删除|
">修改
2-6 web-stu_add.jsp
添加学生界面
姓名
年龄
班级
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
修改学生
<input type="hidden" name="id" value="">
姓名
<input type="text" name="name" value="">
年龄
<input type="text" name="age" value="">
班级
<input type="text" name="classes" value="">
三、总结
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 没有写登录页面。