先来看效果:
1.展示和添加页面
2.登录
这是我学习javaEE时用原生技术写的一个简单的增、删、改、查的demo,如有错误请大家指正!
需要用到的jar包:
这里红框框住的是自己做的原生的BaseDao的jar包,下一篇文章会详细说明。
CRUD:
就是对数据的一系类操作:前端页面获取数据,后台处理数据,到数据库查询,返回数据给前端页面。
下面上代码:
前端代码:
index.jsp
源代码学生信息管理
学生信息管理(添加学生)
编号
姓名
年龄
性别
专业
成绩
操作
${student.id }
${student.name }
${student.age }
${student.sex }
${student.major }
${student.grade }
删除 修改
listStudent.jsp
源代码学生信息管理
学生信息管理(添加学生)
编号
姓名
年龄
性别
专业
成绩
操作
${student.id }
${student.name }
${student.age }
${student.sex }
${student.major }
${student.grade }
删除 修改
addStudent.jsp
添加学生信息
添加学生信息
后台代码:
Student.java
public class Student {
@Id
private Integer id;
private String name;
private Integer age;
private String sex;
private String major;
private String grade;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Student() {
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", major=" + major
+ ", grade=" + grade + "]";
}
}
IStudentDao.java
public interface IStudentDao extends IBaseDao {
int getRecordsNum();
}
StudentDaoImpl.java
public class StudentDaoImpl extends BaseDaoImpl implements IStudentDao{
public StudentDaoImpl() {
super(Student.class);
}
@Override
public int getRecordsNum() {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int recordsNum=0;
try {
con=JDBCUtil.intstance.getConn();
String sql="select count(*) count from student";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
recordsNum=rs.getInt("count");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
JDBCUtil.intstance.close(con, ps, null);
} catch (Exception e) {
e.printStackTrace();
}
}
return recordsNum;
}
public List findByPageNum(Page page){
int begin=(page.getPageNum()-1)*page.getPageSize();
List sudentList=new ArrayList();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=JDBCUtil.intstance.getConn();
String sql="select * from student limit ?,?";
ps=con.prepareStatement(sql);
ps.setInt(1, begin);
ps.setInt(2, page.getPageSize());
rs=ps.executeQuery();
while(rs.next()){
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getString("sex"));
student.setMajor(rs.getString("major"));
student.setGrade(rs.getString("grade"));
sudentList.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
JDBCUtil.intstance.close(con, ps, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
return sudentList;
}
}
ControllerServlet.java
@WebServlet("*.do")
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private StudentDaoImpl sd = new StudentDaoImpl();
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String url=request.getRequestURI();
String action=request.getRequestURI().substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
if("list".equals(action)){
Page page=new Page();
String pageNum=request.getParameter("pageNum");
if(pageNum==null){
page.setPageNum(1);
}else{
page.setPageNum(Integer.parseInt(pageNum));
}
List listStudent=sd.findByPageNum(page);
request.setAttribute("listStudent", listStudent);
request.setAttribute("page", page);
request.getRequestDispatcher("listStudent.jsp").forward(request, response);
}
else if("add".equals(action)){
Student student = new Student();
student.setName(request.getParameter("name"));
student.setAge(Integer.parseInt(request.getParameter("age")));
student.setSex(request.getParameter("sex"));
student.setMajor(request.getParameter("major"));
student.setGrade(request.getParameter("grade"));
sd.add(student);
response.sendRedirect("list.do");
}else if("delete".equals(action)){
int id=Integer.parseInt(request.getParameter("id"));
sd.delect(id);
response.sendRedirect("list.do");
}else if("load".equals(action)){
Student student = sd.queryOne(Integer.parseInt(request.getParameter("id")));
request.setAttribute("student",student);
request.getRequestDispatcher("studentInfo.jsp").forward(request, response);
}else if("update".equals(action)){
Student student = new Student();
student.setId(Integer.parseInt(request.getParameter("id")));
student.setName(request.getParameter("name"));
student.setAge(Integer.parseInt(request.getParameter("age")));
student.setSex(request.getParameter("sex"));
student.setMajor(request.getParameter("major"));
student.setGrade(request.getParameter("grade"));
sd.updata(student);
response.sendRedirect("list.do");
}
}
}
以上就是关键代码,后面我会将这个demo放在码云上,以供大家参考。