接着上一篇,环境配置好以后,就要开始开发了,在使用servlet+jsp开发的项目时候,必然会操涉及到操作数据库,这篇文章我就总结一下这些内容。
注:在开始写代码之前,我们必须要准备jar包,连接池工具,连接池配置文件,新建五个包,分别是bean,servlet,service,dao,还有你的连接池工具包
查询操作
bean:创建实体类对象,所有的字段和数据类型都必须和数据库里一一对应,
package com.yf.bean;
public class Student {
private String student_id;
private String student_num;
private String password;
private String student_name;
private int student_sex;
private String phone;
private String address;
private String id_card;
private String faculty;
private String pricture_url;
private String major;
private String class_;
private int age;
private String class_table;
public String getClass_table() {
return class_table;
}
public void setClass_table(String class_table) {
this.class_table = class_table;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_num() {
return student_num;
}
public void setStudent_num(String student_num) {
this.student_num = student_num;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public int getStudent_sex() {
return student_sex;
}
public void setStudent_sex(int student_sex) {
this.student_sex = student_sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getId_card() {
return id_card;
}
public void setId_card(String id_card) {
this.id_card = id_card;
}
public String getFaculty() {
return faculty;
}
public void setFaculty(String faculty) {
this.faculty = faculty;
}
public String getPricture_url() {
return pricture_url;
}
public void setPricture_url(String pricture_url) {
this.pricture_url = pricture_url;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getClass_() {
return class_;
}
public void setClass_(String class_) {
this.class_ = class_;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
servlet:获取前台发回来的数据,将数据传递到service层中进行操作
/**
* 查询操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void Select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//创建StudentService
StudentService ss = new StudentService();
//调用方法
List list = ss.findAll();
//把list集合放入request域对象中
request.setAttribute("list", list);
//请求转发到list.jsp
request.getRequestDispatcher("/studentlist.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("msg", "查询学生出现错误");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
service:对servlet传入的数据再次处理,查询中没有处理操作,所以直接把数据传到dao中
/**
* 查询
* @return
* @throws SQLException
*/
public List findAll() throws SQLException {
// TODO Auto-generated method stub
StudentDao sd = new StudentDao();
return sd.findAll();
}
dao:这一层主要对数据库进行操作,将数据保存到数据库
/**
* 查询
* @return
* @throws SQLException
*/
public List findAll() throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select * from student";
List query = qr.query(sql, new BeanListHandler(Student.class));
return query;
}
studentlist页面:将查询出的数据展示出来
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Insert title here
图片
学生id
学号
密码
姓名
性别
手机号码
家庭地址
身份证号码
院系
专业
班级
年龄
操作
暂无信息
${stu.student_id}
${stu.student_num}
${stu.password}
${stu.student_name}
${stu.student_sex}
${stu.phone}
${stu.address}
${stu.id_card}
${stu.faculty}
${stu.major}
${stu.class_}
${stu.age}
修改
删除
增加操作
前端增加值页面 :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
返回主页面
增加学生信息表
请务必完整填写信息!
servlet:获取前台发回来的数据,将数据传递到service层中进行操作
/**
* 增加操作
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void Add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//获取前台录入的数据map
Map map =request.getParameterMap();
//创建bean
Student stu = new Student();
//把map的数据拷入bean
BeanUtils.populate(stu, map);
//把pid和pdata放入bean
stu.setStudent_id(UUIDcls.getUUID());
//调用service完成保存操作
StudentService ss=new StudentService();
ss.saveStudent(stu);
//请求转发到list.jsp
request.getRequestDispatcher("/StudentServlet?method=select").forward(request, response);
//注意一下这儿是重定向到查询中,这样就可以马上刷新新增的那条代码了
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("msg", "增加学生出现错误");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
service:对servlet传入的数据再次处理,增加中没有处理操作,所以直接把数据传到dao中
/**
* 增加
* @param stu
* @throws SQLException
*/
public void saveStudent(Student stu) throws SQLException {
// TODO Auto-generated method stub
StudentDao sd= new StudentDao();
sd.saveStudent(stu);
}
dao:这一层主要对数据库进行操作,将数据保存到数据库
/**
* 增加学生信息
*/
//创建QueryRunner
QueryRunner qr =new QueryRunner(DataSourceUtils.getDataSource());
//创建sql
String sql = "insert into student (student_id,student_num,Password,Student_name,Student_sex,Phone,Address,Id_card,Faculty,Pricture_url,Major,Class_,Age)value(?,?,?,?,?,?,?,?,?,?,?,?,?)";
//执行sql,注意此处调用的方法是update
qr.update(sql, stu.getStudent_id(),stu.getStudent_num(),stu.getPassword(),stu.getStudent_name(),stu.getStudent_sex(),stu.getPhone(),stu.getAddress(),
stu.getId_card(),stu.getFaculty(),stu.getPricture_url(),stu.getMajor(),stu.getClass_(),stu.getAge());
}
修改操作
注:修改操作整体需要两步操作,第一步,根据id获取对应的数据,第二步,将修改过的数据进行保存
studentlist页面:继续使用增加操作中使用过的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Insert title here
修改学生信息表
返回主页面
studentedit页面:用于修改数据的页面
servlet:里面有两个步骤
/**
* 修改操作第一步,获取对应id的数据
* @throws IOException
* @throws ServletException
*/
private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
//得到id
String id = request.getParameter("id");
//创建service
StudentService ss = new StudentService();
Student stu= ss.getProById(id);
//把stu放入request
request.setAttribute("stu",stu);
//请求转发edit
request.getRequestDispatcher("/studentedit.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("msg","查询单条记录商品失败");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
/**
* 修改操作第二步,将修改后的数据进行保存
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
//获取map
Map map = request.getParameterMap();
//创建bean
Student stu=new Student();
//把map中的数据拷贝到bean
BeanUtils.populate(stu,map);
//调用service完成数据的更新
StudentService ss = new StudentService();
ss.updateStudent(stu);
//请求转发到所有商品的链接上
request.getRequestDispatcher("/StudentServlet?method=select").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("msg", "更新保存时失败");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
service:里面对应servlet也有两个方法
/**
* 修改操作第一步,根据id得到对应的数据
* @param id
* @return
* @throws SQLException
*/
public Student getProById(String id) throws SQLException {
// TODO Auto-generated method stub
StudentDao sd= new StudentDao();
return sd.getProById(id);
}
/**
* 修改操作第二步,把修改过的数据进行保存
* @param stu
* @throws SQLException
*/
public void updateStudent(Student stu) throws SQLException {
// TODO Auto-generated method stub
StudentDao sd = new StudentDao();
sd.updateStudent(stu);
}
dao:对应service的两个方法
/**
* 修改操作第一步。根据id获取对应的值
* @param id
* @return
* @throws SQLException
*/
public Student getProById(String id) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select * from student where student_id =?";
Student query = qr.query(sql, new BeanHandler(Student.class), id);
return query;
}
/**
* 修改操作第二步,把修改过的数据进行保存
* @param stu
* @throws SQLException
*/
public void updateStudent(Student stu) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql="update student set student_num=?,password=?,student_name=?,student_sex=?,phone=?,address=?,id_card=?,faculty=?,pricture_url=?,major=?,class_=?,age=? where student_id=?";
qr.update(sql,stu.getStudent_num(),stu.getPassword(),stu.getStudent_name(),stu.getStudent_sex(),stu.getPhone(),stu.getAddress(),stu.getId_card(),stu.getFaculty(),stu.getPricture_url(),stu.getMajor(),stu.getClass_(),stu.getAge(),stu.getStudent_id());
}
删除操作
studentlist页面:依然使用增加操作的那个页面
servlet页面:
/**
* 删除操作
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// TODO Auto-generated method stub
String id = request.getParameter("pid");
StudentService ss = new StudentService();
ss.delete(id);
//请求转发到查询页面
request.getRequestDispatcher("/StudentServlet?method=select").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("msg", "删除操作失败");
request.getRequestDispatcher("/error").forward(request, response);
}
}
service页面:
/**
* 删除操作
* @param id
* @throws SQLException
*/
public void delete(String id) throws SQLException {
// TODO Auto-generated method stub
StudentDao sd = new StudentDao();
sd.delete(id);
}
dao页面:
/**
* 删除操作
* @param id
* @return
* @throws SQLException
*/
public void delete(String id) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql="delete from student where student_id=?";
qr.update(sql,id);
}
好了,基本的操作就全都写完了,需要注意的一点是,我们在创建方法时要注意,传递的参数类型和个数,以及实体类bean和数据库的关系,获取前端页面数据时使用的方法,下一篇会写微信小程序和java后台的数据传输。