guanerxiaoyu
一只学霸吖
采纳率0%
2020-04-14 18:14 阅读 940

#做一个简单的学生信息管理系统,实现增删改查的功能

还是新手小白,项目代码报错不会改,请求大佬的帮助
做一个简单的学生信息管理系统,实现增删改查的功能

StudentDao

package com.hdxy.Dao;
import java.sql.Connection;
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 com.hdxy.Student.Student;
public class StudentDao {
//初始化Connection,Statement,ResultSet
Connection conn = null;
Statement st = null;
ResultSet rs = null;

//查询
public List<Student> testQuery(){
    //查询表中所有学生,用集合来接收
    List<Student> list = new ArrayList<Student>();        
    String sql ="select * from student order by id asc";
    Connection conn = JDBCUtil.getInstance().getConnection();//获取连接
    try {
        st = conn.createStatement();//预处理,创建集合接收数据
        rs = st.executeQuery(sql);//结果集,创建集合接收数据
        //循环遍历
        while(rs.next()){
            Student student = new Student();//实例化Student对象,要求要有Student对象
            student.setId(rs.getInt("id"));//对象.方法或对象.属性。setI()想让id在前台显示打包赋值给student对象,getInt()获取学生信息
            student.setName(rs.getString("name"));
            student.setSno(rs.getString("sno"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getString("age"));
            list.add(student);//将student对象的数据传到list里
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    JDBCUtil.getInstance().closeConnection(rs,st,conn);//关闭,没有学生了就停止循环遍历
    return list;//返回值类型return,返回list
}//testQuery结束后拿到了所有学生的属性值

//添加数据
public void saveStudent(Student student){
    //?:表示占位符.所谓占位符就是不知道要传递什么值,但是必须要有值
    String sql = "insert into student(sno,name,sex,age) values(?,?,?,?)";

    conn = JDBCUtil.getInstance().getConnection();
    try {
        PreparedStatement  pst = conn.prepareStatement(sql);
        pst.setString(1, student.getSno());
        pst.setString(2, student.getName());
        pst.setString(3, student.getSex());
        pst.setString(4, student.getAge());
        pst.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    JDBCUtil.getInstance().closeConnection(rs,st,conn);
}


public Student getStudent(int id){
    Student stu = new Student();
    String sql = "select id,sno,name,sex,age from student where id=?";
    Connection conn = JDBCUtil.getInstance().getConnection();//获取连接
    try {
        PreparedStatement  st = conn.prepareStatement(sql);
        st.setInt(1, id);
        rs = st.executeQuery();
        while(rs.next()){
            stu.setId(rs.getInt("id"));
            stu.setSno(rs.getString("sno"));
            stu.setName(rs.getString("name"));
            stu.setSex(rs.getString("sex"));
            stu.setAge(rs.getString("age"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    JDBCUtil.getInstance().closeConnection(rs,st,conn);
    return stu;
}

//更改数据
public void updateStudent(Student student){
    String sql = "update student set sno=?,name=?,sex=?,age=? where id=?";
    Connection conn = JDBCUtil.getInstance().getConnection();//获取连接
    try {
        PreparedStatement  st = conn.prepareStatement(sql);
        st.setString(1, student.getSno());
        st.setString(2, student.getName());
        st.setString(3, student.getSex());
        st.setString(4, student.getAge());
        st.setInt(5, student.getId());
        st.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    JDBCUtil.getInstance().closeConnection(rs,st,conn);
}

//删除数据
public void deleteStudent(int id){
    String sql = "delete from  student where id=?";
    Connection conn = JDBCUtil.getInstance().getConnection();//获取连接
    try {
        PreparedStatement  st = conn.prepareStatement(sql);
        st.setInt(1, id);
        st.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    JDBCUtil.getInstance().closeConnection(rs,st,conn);
}

}

JDBC的工具类

package com.hdxy.Dao;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Connection;
//JDBC的工具类
public class JDBCUtil {
//一.初始化数据库相关信息对象
static String driverClass="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://localhost:3306/student?characterEncoding=utf-8";;
static String name="root";
static String password="root";
static Connection conn = null;
private static JDBCUtil instance = null;

private JDBCUtil(){

}

public static JDBCUtil getInstance(){
    if(instance == null){
        synchronized (JDBCUtil.class) {
            if(instance == null){
                instance = new JDBCUtil();
            }
        }
    }
    return instance;

}
//二.读取jdbc.properties文件并把相关信息赋予对象
    static{
        try{
            String driver = null;
            Class.forName(driver);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

//三.获取连接对象
public Connection getConnection(){
    try{        
        conn= (Connection) DriverManager.getConnection(url,name,password);
    }catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

public void closeConnection(ResultSet rs, Statement st, java.sql.Connection conn) {
    try {
        if(rs != null){
            rs.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally{
        try{
            if(st != null){
                st.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(conn != null){
                    conn.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

}

服务类

package com.hdxy.Service;

import java.util.List;

import com.hdxy.Dao.StudentDao;
import com.hdxy.Student.Student;
//服务类
public class StudentService {

public List<Student> testQuery(){
    StudentDao studentDao = new StudentDao();//获取StudentDao实现类
    List<Student> list = studentDao.testQuery();
    return list;//返回值类型return,返回list
}

public void saveStudent(Student student){
    StudentDao studentDao = new StudentDao();
    studentDao.saveStudent(student);
}

public Student getStudent(int id){
    StudentDao studentDao = new StudentDao();
    return studentDao.getStudent(id);
}

public void deleteStudent(int id){
    StudentDao studentDao = new StudentDao();
    studentDao.deleteStudent(id);
}

public void updateStudent(Student student){
    StudentDao studentDao = new StudentDao();
    studentDao.updateStudent(student);
}

}

实体类

package com.hdxy.Student;

public class Student {
private int id;
private String sno;
private String name;
private String sex;
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Student(int id, String sno, String name, String sex, String age) {
super();
this.id = id;
this.sno = sno;
this.name = name;
this.sex = sex;
this.age = age;
}
public Student() {

}
@Override
public String toString() {
    return "Student [id=" + id + ", sno=" + sno + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
}

}

Servlet

package com.hdxy.Servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hdxy.Student.Student;
import com.hdxy.Service.StudentService;

public class StudentServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}   


@Override
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    String method = request.getParameter("method");
    if("testQuery".equals(method)){
        testQuery(request,response);
        System.out.println("888");
    }else if("toAdd".equals(method)){
        toAdd(request,response);
    }else if("toEdit".equals(method)){
        toEdit(request,response);
    }else if("saveStudent".equals(method)){
        saveStudent(request,response);
    }else if("updateStudent".equals(method)){
        updateStudent(request,response);
    }else if("deleteStudent".equals(method)){
        deleteStudent(request,response);
    }
    System.out.println("777");
}

private void testQuery(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    StudentService studentService = new StudentService();
    List<Student> list = studentService.testQuery();
    request.setAttribute("testQuery", list);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/list.jsp");
    dispatcher.forward(request, response);
    System.out.println("111");
}


private void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    RequestDispatcher dispatcher = request.getRequestDispatcher("/add.jsp");
    dispatcher.forward(request, response);
    System.out.println("222");
}

private void toEdit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    int id = Integer.parseInt(request.getParameter("id"));
    StudentService studentService = new StudentService();
    Student student = studentService.getStudent(id);
    request.setAttribute("student", student);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/edit.jsp");
    dispatcher.forward(request, response);
    System.out.println("333");
}

private void saveStudent(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

    Student student = new Student();
    student.setSno(request.getParameter("sno"));
    student.setName(request.getParameter("name"));
    student.setSex(request.getParameter("sex"));
    student.setAge(request.getParameter("age"));

    StudentService studentService = new StudentService();
    studentService.saveStudent(student);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
    System.out.println("444");
}

private void updateStudent(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    Student student = new Student();
    student.setSno(request.getParameter("sno"));
    student.setName(request.getParameter("name"));
    student.setSex(request.getParameter("sex"));
    student.setId(Integer.parseInt(request.getParameter("id")));
    student.setAge(request.getParameter("age"));

    StudentService studentService = new StudentService();
    studentService.updateStudent(student);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
    System.out.println("555");
}

private void deleteStudent(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    int id = Integer.parseInt(request.getParameter("id"));
    StudentService studentService = new StudentService();
    studentService.deleteStudent(id);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    dispatcher.forward(request, response);
    System.out.println("666");
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐