应用实例:用三层架构模型实现一个增加学生功能的学生信息管理项目
涉及的理论知识:三层架构设计模型
1.需要用到的数据库与数据表
create database test;
use test;
drop table if exists student;
create table student(
s_id int not null primary key,
s_name varchar(20),
s_age int,
s_address varchar(20)
)
select * from student;
2.将数据库驱动jar包复制到目录WebContent/WEB-INF/lib下,选中改包:右键build path—>add build path
3.在WebContent的目录下新建一个addstudent.jsp和success.jsp页面
addstudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
添加成功!!!
4.在src目录下新建com.xiaochen.entity包中新建实体类Student
Student.java
package com.xiaochen.entity;
public class Student {
private int id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
5.在src目录下新建com.xiaochen.dao包中新建实体类StudentDao
StudentDao.java
package com.xiaochen.dao;
import java.sql.*;
import com.xiaochen.entity.Student;
public class StudentDao {
private static final String DRIVER="com.mysql.jdbc.Driver";
private static final String URL="jdbc:mysql://localhost:3306/test";
private static final String NAME="root";
private static final String PWD="123abc";
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
int count=-1;
/*
* 查询学生是否存在:通过学号查询
return student:登录成功,返回该学生信息
return null:登录失败,系统异常,返回null
*/
public Student queryById(Student student) {
try {
//加载具体的驱动类
Class.forName(DRIVER);
//连接数据库
con=DriverManager.getConnection(URL,NAME,PWD);
//获取操作数据库对象
String sql="select * from student where s_id=?";
pstmt=con.prepareStatement(sql);
//给占位符赋值
pstmt.setInt(1, student.getId());
//返回结果集
rs=pstmt.executeQuery();
while(rs.next()!=false) {
//若存在该学生则将返回值赋予count
count=rs.getInt(student.getId());
}if(count>0)//登录成功
return student;
else//登录失败
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
/*
* 判断学生是否存在
调用通过学号查询学生方法来判断
*/
public boolean isExtis(Student student) {
if(queryById(student)!=null)//表示登录成功,则存在该学生
return true;
else表示登录失败,该学生不存在
return false;
}
/*增加学生功能
* return true:增加成功
* return false:系统异常,增加失败
*/
public boolean addStudent(Student student) {
try {
//加载具体的驱动类
Class.forName(DRIVER);
//连接数据库
con=DriverManager.getConnection(URL,NAME,PWD);
//获取操作数据库对象
String sql="insert into student (s_id,s_name,s_age,s_address) values(?,?,?,?)";
pstmt=con.prepareStatement(sql);
//给占位符赋值
pstmt.setInt(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getAddress());
//操作数据库
pstmt.executeUpdate();
//若成功添加数据,则返回该学生
return true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}catch (Exception e) {
e.printStackTrace();
return false;
}finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
}
6.在src目录下新建com.xiaochen.servlet包中新建一个StudentServlet
StudentServlet.java
package com.xiaochen.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xiaochen.dao.StudentDao;
import com.xiaochen.entity.Student;
/**
* Servlet implementation class StudentServlet
*/
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
//设置统一编码格式
request.setCharacterEncoding("utf-8");
//接收jsp页面传来的表单数据
int id=Integer.parseInt(request.getParameter("sid"));
String name=request.getParameter("sname");
int age=Integer.parseInt(request.getParameter("sage"));
String address=request.getParameter("saddress");
//将接收的数据封装成一个实体类(即封装数据的javabean)
Student student=new Student(id,name,age,address);
//操作StudentDao来实现增加学生的功能
StudentDao studentdao=new StudentDao();
boolean result=studentdao.addStudent(student);
if(result!=false);
//增加成功后跳转到success.jsp页面
response.sendRedirect("success.jsp");
}
}