一、MVC架构
- bean层:一种封装数据的模型,在Java中的具体体现就是一个数据类的定义
- action层:又叫表示层,在Java中的具体体现就是前台的设计及交互
- dao层:又叫持久层,主要为前台提供数据
- service层:又叫服务层,在dao层基础上增加业务逻辑
- util层:又叫工具层
查询所有学生信息
一,先导入jar包
- bean层(不变)是用来关联数据库表格的
- util层来打开关闭数据库的
基本上一样,只要把下面的数据库名字改了就可以了。
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//打开关闭数据库
public class DBUtil {
static Connection conn;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
//那个edu就是数据库,哪个数据库连接哪个。
String url="jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=UTF-8";
String userName="root";//自己数据库的账号
String password="";//自己数据库的密码
conn=DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement stmt,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
Connection conn=DBUtil.getConnection();
if(conn!=null){
System.out.println("数据库连接成功");
}else{
System.out.println("数据库连接失败");
}
}
}
- dao层为前台提供数据
package dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Student;
import util.DBUtil;
public class StudentDao {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
List<Student> stu=null;
int n=0;
//查询所有的学生信息;
public List<Student> selectAllStudent(){
try {
//用静态类就可以这样调用
conn=DBUtil.getConnection();
stmt=conn.createStatement();
String sql="select * from student";
rs= stmt.executeQuery(sql);
//这里定义一个列表,把结果集中的Student的对象存放列表中
stu=new ArrayList<Student>();
while(rs.next()){
//把结果集当前的数据转成一个Student对象
Student s=new Student();
s.setS_id(rs.getString("s_id"));
s.setS_name(rs.getString("s_name"));
s.setS_sex(rs.getBoolean("s_sex"));
s.setS_birthday(rs.getDate("s_birthday"));
s.setS_major(rs.getString("s_major"));
s.setS_scholarship(rs.getInt("s_scholarship"));
//把对象存到列表
stu.add(s);
// String xh= rs.getString("s_id");
// String xm=rs.getString("s_name");
// String xb=rs.getString("s_sex");
// Date csrq =rs.getDate("s_birthday");
// System.out.println("xh="+xh+",xm="+xm+",xb="+xb+",csrq="+csrq);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
//根据姓名模糊查询相关学生信息
public List<Student> selectStudentLikeName(String xm) {
try {
//用静态类就可以这样调用
conn=DBUtil.getConnection();
String sql="select * from student where s_name like ?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%".concat(xm).concat("%"));
rs= pstmt.executeQuery();
//这里定义一个列表,把结果集中的Student的对象存放列表中
stu=new ArrayList<Student>();
while(rs.next()){
//把结果集当前的数据转成一个Student对象
Student s=new Student();
s.setS_id(rs.getString("s_id"));
s.setS_name(rs.getString("s_name"));
s.setS_sex(rs.getBoolean("s_sex"));
s.setS_birthday(rs.getDate("s_birthday"));
s.setS_major(rs.getString("s_major"));
s.setS_scholarship(rs.getInt("s_scholarship"));
//把对象存到列表
stu.add(s);
// String xh= rs.getString("s_id");
// String xm=rs.getString("s_name");
// String xb=rs.getString("s_sex");
// Date csrq =rs.getDate("s_birthday");
// System.out.println("xh="+xh+",xm="+xm+",xb="+xb+",csrq="+csrq);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
//添加学生信息
//修改学生信息;
//删除学生信息;
public int insertStudent(Student s) {
// TODO Auto-generated method stub
try {
//用静态类就可以这样调用
conn=DBUtil.getConnection();
String sql="insert into student values(?,?,?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, s.getS_id());
pstmt.setString(2, s.getS_name());
pstmt.setBoolean(3, s.isS_sex());
//把util.Date数据类型转成sql.Date的数据类型
pstmt.setDate(4, new Date(s.getS_birthday().getTime()));
pstmt.setString(5, s.getS_major());
pstmt.setInt(6, s.getS_scholarship());
n=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("n="+n);
return n;
}
}
- service层,在dao层基础上增加业务逻辑
里面的有3个项目的代码
package service;
import java.util.List;
import bean.Student;
import dao.StudentDao;
public class StudentService {
StudentDao studentDao;
public StudentService(){
studentDao=new StudentDao();
}
//查询整张表格的数据
public List<Student> selectAllStudent(){
List<Student> stu=studentDao.selectAllStudent();
return stu;
}
//模糊查询的学生的数据
public List<Student> selectStudentLikeName(String xm) {
return studentDao.selectStudentLikeName(xm);
}
//插入学生数据代码
public int insertStudent(Student s) {
// TODO Auto-generated method stub
return studentDao.insertStudent(s);
}
}
- action层,又叫表示层,在Java中的具体体现就是前台的设计及交互
- 查询整张表格的代码
try {
// StudentDao studentDao=new StudentDao();
// List<Student> stu=studentDao.selectAllStudent();
StudentService studentService=new StudentService();
List<Student> stu=studentService.selectAllStudent();
//把列表存到域对象
request.setAttribute("stus", stu);
RequestDispatcher dispatcher=request.getRequestDispatcher("selectResult.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.close(conn, stmt, rs);
}
- 模糊查询学生的代码
request.setCharacterEncoding("utf-8");
String xm=request.getParameter("name");
//调用StudentSeivlet层的方法来完成模糊查询
StudentService studentService=new StudentService();
List<Student> stu=studentService.selectStudentLikeName(xm);
request.setAttribute("stus", stu);
RequestDispatcher dispatcher=request.getRequestDispatcher("selectResult.jsp");
dispatcher.forward(request, response);
- 插入表格数据的代码
request.setCharacterEncoding("utf-8");
String xh=request.getParameter("xh");
String xm=request.getParameter("xm");
String xb=request.getParameter("xb");
String csrq=request.getParameter("csrq");
String zy=request.getParameter("zy");
String jxj=request.getParameter("jxj");
Student s=new Student();
s.setS_id(xh);
s.setS_name(xm);
if(xb.equals("男")){
s.setS_sex(true);
}
if(xb.equals("女")){
s.setS_sex(false);
}
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = format.parse(csrq);
s.setS_birthday(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.setS_major(zy);
s.setS_scholarship(Integer.parseInt(jxj));
StudentService studentService=new StudentService();
int n=studentService.insertStudent(s);
System.out.println("servlet里面的n="+n);
- web界面