前端
jsp JSTL EL bootStracp
后端
java c3p0
数据库
SQL
将main界面分为三部分,里面包含top、left、right三个界面,使用bootstracp进行修饰界面
SMS学生管理系统
上面界面部分
左面界面部分
菜单
右面界面部分
分为三个界面添加、修改(和添加界面类似,可整合)、显示,分别为add.jsp、edit.jsp、list.jsp,为了安全性,不在网址直接访问到,将所有界面写入web下的WEB-INF下,采用servlet跳转的方式访问
添加界面
添加
修改界面
<%--修改<%=request.getAttribute("student")%> ${student}
--%>
修改
显示界面
新建StudentDaolimpl类,编写对数据库分别进行增、删、改、查(查单个,查多个)操作,分别写对应的sql语句,然后调用utils中的update()方法进行验证是否修改数据成功。
注意:各个方法的传入参数以及返回值。
public class StudentDaoImpl implements StudentDaoInte {
@Override
public int insert(Student student) {
String sql = "insert into students(name,age,gender,tel,class_id) value(?,?,?,?,?)";
String[] args = {student.getName(),student.getAge()+"",student.getGender()+"",student.getTel(),student.getClass_id()+""};
return DBUtil.update(sql,args);
}
@Override
public int delete(int id) {
String sql = "delete from students where id=?";
String[] args = {id+""};
return DBUtil.update(sql,args);
}
@Override
public int update(Student student) {
String sql = "update students set name=?,age=?,gender=?,tel=?,class_id=? where id=?";
System.out.println("student = " + student);
String[] args = {student.getName(),student.getAge()+"",student.getGender()+"",student.getTel(),student.getClass_id()+"",student.getId()+""};
return DBUtil.update(sql,args);
}
@Override
public List select() {
String sql = "select * from students";
BeanListHandler handler = new BeanListHandler(Student.class);
String[] args = {};
return DBUtil.select(sql,handler,args);
}
@Override
public Student find(int id) {
Student student = null;
String sql = "select * from students where id=?";
BeanListHandler handler = new BeanListHandler(Student.class);
String[] args = {id+""};
List list = DBUtil.select(sql,handler,args);
if(list.size() == 1){
student = list.get(0);
}
return student;
}
}
新建Student类,编写对应的属性(用private修饰),然后生成构造器、get、set、toString、方法
student.java
public class Student {
private int id;
private String name;
private int age;
private int gender;
private String tel;
private int class_id;
// private Classes classes;
public Student() {
}
public Student(int id, String name, int age, int gender, String tel, int class_id) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.tel = tel;
this.class_id = class_id;
}
public Student(String name, int age, int gender, String tel, int class_id) {
this.name = name;
this.age = age;
this.gender = gender;
this.tel = tel;
this.class_id = class_id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", tel='" + tel + '\'' +
", class_id=" + class_id +
'}';
}
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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getClass_id() {
return class_id;
}
public void setClass_id(int class_id) {
this.class_id = class_id;
}
}
新建StudentServiceImpl类,编写增、删、改、查(查单个,查多个)方法,调用dao中对应的方法,取得返回值,将返回值进行判断。
public class StudentServiceImpl implements StudentServiceInte {
private StudentDaoInte sdi = new StudentDaoImpl();
@Override
public boolean add(Student student) {
int rows = sdi.insert(student);
return rows>0 ? true : false;
}
@Override
public boolean remove(int id) {
int rows = sdi.delete(id);
return rows>0 ? true : false;
}
@Override
public boolean edit(Student student) {
int rows = sdi.update(student);
return rows>0 ? true : false;
}
@Override
public List queryAll() {
return sdi.select();
}
@Override
public Student queryOne(int id) {
return sdi.find(id);
}
}
新建两个类IndexServlet类和StudentServlet类
IndexServlet类:访问主界面
StudentServlet类:根据传入参数a的值,进行对应的增、删、改、查界面,并传入相应的参数
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String a = req.getParameter("a");
if(a != null){
req.getRequestDispatcher("/WEB-INF/sms/"+a+".jsp").forward(req,resp);
}
}
}
@WebServlet("/stu")
public class StudentServlet extends HttpServlet {
private StudentServiceInte ssi = new StudentServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("a");
switch (action){
case "add":
//get请求add添加,跳转到add页面
req.getRequestDispatcher("/WEB-INF/sms/stu/add.jsp").forward(req,resp);
break;
case "edit"://跳转到编辑页面,并且显示原来的值
//先 取到 原来的值
int edit_id = Integer.parseInt(req.getParameter("id"));
Student student = ssi.queryOne(edit_id);
if(student == null){
return;
}
//传到 要跳转的页面,跳转过去
req.setAttribute("student",student);
req.getRequestDispatcher("/WEB-INF/sms/stu/edit.jsp").forward(req,resp);
break;
case "del":
int del_id = Integer.parseInt(req.getParameter("id"));
boolean flag = ssi.remove(del_id);
if(flag){
String path = req.getRequestURI()+"?a=look";
resp.sendRedirect(path);
}else{
resp.getWriter().print("");
}
break;
case "look":
List list = ssi.queryAll();
req.setAttribute("list",list);
req.getRequestDispatcher("/WEB-INF/sms/stu/list.jsp").forward(req,resp);
break;
default:break;
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("a");
switch (action){
case "add":
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
int gender = Integer.parseInt(req.getParameter("gender"));
String tel = req.getParameter("tel");
int class_id = Integer.parseInt(req.getParameter("class_id"));
Student student = new Student(name,age,gender,tel,class_id);
//调用业务层
boolean flag = ssi.add(student);
if(flag){
String path = req.getRequestURI()+"?a=look";
resp.sendRedirect(path);
}else{
resp.getWriter().print("");
}
break;
case "edit"://将修改后的表单数据,更新到数据库里
//接受表单数据
int e_id = Integer.parseInt(req.getParameter("id"));
String e_name = req.getParameter("name");
int e_age = Integer.parseInt(req.getParameter("age"));
int e_gender = Integer.parseInt(req.getParameter("gender"));
String e_tel = req.getParameter("tel");
int e_class_id = Integer.parseInt(req.getParameter("class_id"));
Student e_student = new Student(e_id,e_name,e_age,e_gender,e_tel,e_class_id);
boolean e_flag = ssi.edit(e_student);
if(e_flag){
String path = req.getRequestURI()+"?a=look";
resp.sendRedirect(path);
}else{
resp.getWriter().print("");
}
break;
default:break;
}
}
}
新建两个类,DBUtil类和JDBCUtil类
DBUtil类:判断与数据库的操作,以及返回操作结果。
JDBCUtil类:使用c3p0连接池的方式进行与数据库的连接
public class DBUtil {
private static QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource()); ;
//增删改
public static int update(String sql,Object... args){
int rows = 0;
try {
rows = runner.update(sql,args);
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
public static List select(String sql, BeanListHandler beanListHandler, Object... args){
List list = null;
try {
list = runner.query(sql,beanListHandler,args);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
public class JDBCUtil {
private static DataSource dataSource;
private static Connection connection;
public static DataSource getDataSource(){
if(dataSource == null){
//自己主动 去 找 c3p0-config.xml 文件
dataSource = new ComboPooledDataSource("myConfig");
}
return dataSource;
}
public static Connection getConnection(){
if (connection == null) {
try {
connection = getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
public static void closeConnection(){
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
c3p0-config.xml
10
30
100
10
200
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/sms
root
root
10
30
100
10
200
......
......
建两个表student表、classes表
设置所需的列名,并将class_id设置为classes表的外键
CREATE TABLE `NewTable` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`age` tinyint(3) UNSIGNED NULL DEFAULT 1 ,
`gender` tinyint(4) NULL DEFAULT 1 COMMENT '1男0女' ,
`tel` char(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`class_id` int(11) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
INDEX `class_id` (`class_id`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=4
ROW_FORMAT=COMPACT
;
设置所需的列名
CREATE TABLE `NewTable` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`num` tinyint(4) NULL DEFAULT 0 ,
`major` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX `major` (`major`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1002
ROW_FORMAT=COMPACT
;