5.29

发布时间 2023-06-11 08:52:15作者: 意い十三章

今日对昨天考试的功能又增加了一些,完成了在考试的时候没有完成的功能:

代码如下:

Dao.java

复制代码
package dao;

import Util.DBUtil;
import bean.Sbean;
import bean.Tbean;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Dao {

    public boolean add(Sbean ten) throws ClassNotFoundException , SQLException
    {
        String sql="insert into student(num,name,banji,sex,nianji)values"
                + "('" + ten.getNum() + "','" + ten.getName() + "','"+ ten.getBanji() + "','" + ten.getSex()+ "','"+ten.getNianji() + "')";

        Connection conn= DBUtil.getConnection();
        Statement state=null;
        boolean f=false;
        int a = 0;
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            DBUtil.close(state, conn);
        }
        if(a>0)
            f=true;
        return f;
    }
    public boolean add1(Tbean ten) throws ClassNotFoundException , SQLException
    {
        String sql="insert into teacher(Tnum,Tname,Tsex,course)values"
                + "('" + ten.getTnum() + "','" + ten.getTname() + "','"+ ten.getTsex()+ "','"+ten.getCourse() + "')";

        Connection conn= DBUtil.getConnection();
        Statement state=null;
        boolean f=false;
        int a = 0;
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            DBUtil.close(state, conn);
        }
        if(a>0)
            f=true;
        return f;
    }
    //update
   public boolean update(Sbean b)
    {
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            String sql="update student set name=?,banji=?,sex=?,nianji=? where num=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1, b.getName());
            pstmt.setString(2, b.getBanji());
            pstmt.setString(3, b.getSex());
            pstmt.setString(4, b.getNianji());
            pstmt.setString(5, b.getNum());

            pstmt.executeUpdate();
            return true;
        }
        catch (SQLException | ClassNotFoundException e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }
        finally {
            DBUtil.close(rs, pstmt, con);
        }
        return false;
    }
    public Sbean getbytitle(String name) throws ClassNotFoundException ,SQLException
    {
        String sql = "select * from student where num ='" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Sbean ten = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {


                String title2 = rs.getString("num");
                String zi2 = rs.getString("name");
                String person2 = rs.getString("banji");
                String date2=rs.getString("sex");
                String neirong2=rs.getString("nainji");

                ten = new Sbean(title2, zi2 ,person2 ,date2,neirong2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return ten;
    }

    public boolean update1(Tbean b)
    {
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            String sql="update teacher set Tname=?,Tsex=?,course=? where Tnum=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1, b.getTname());
            pstmt.setString(2, b.getTsex());
            pstmt.setString(3, b.getCourse());
            pstmt.setString(4, b.getTnum());


            pstmt.executeUpdate();
            return true;
        }
        catch (SQLException | ClassNotFoundException e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }
        finally {
            DBUtil.close(rs, pstmt, con);
        }
        return false;
    }
    public Tbean getbytitle1(String name) throws ClassNotFoundException ,SQLException
    {
        String sql = "select * from teacher where Tnum ='" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Tbean ten = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {


                String title2 = rs.getString("Tnum");
                String zi2 = rs.getString("Tname");
                String person2 = rs.getString("banji");
                String date2=rs.getString("sex");


                ten = new Tbean(title2, zi2 ,person2 ,date2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return ten;
    }
    //delete
    public boolean delete(String name) throws SQLException, ClassNotFoundException {
        String sql="delete from student where num='" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        int a = 0;
        boolean f = false;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    //update
    public boolean delete1(String name) throws SQLException, ClassNotFoundException {
        String sql="delete from teacher where Tnum='" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        int a = 0;
        boolean f = false;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    //查找
    public boolean name(String name) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "select num from student where num = '" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return flag;
    }


    public List<Sbean> search(String zi, String date) throws SQLException, ClassNotFoundException {
        String sql = "select * from student where ";


        if (zi != "") {
            sql += "num like '%" +zi+ "%'";
        }



        List<Sbean> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Sbean bean = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String title2 = rs.getString("num");
                String zi2 = rs.getString("name");
                String person2 = rs.getString("banji");
                String date2=rs.getString("sex");
                String neirong2=rs.getString("nianji");
                bean = new Sbean(title2, zi2 ,person2,date2,neirong2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return list;
    }

    public List<Sbean> list() throws SQLException, ClassNotFoundException {
        String sql = "select * from student";
        List<Sbean> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                Sbean bean = null;

                String title2 = rs.getString("num");
                String zi2 = rs.getString("name");
                String person2 = rs.getString("banji");
                String date2=rs.getString("sex");
                String neirong2=rs.getString("nianji");

                bean = new Sbean(title2, zi2 ,person2,date2,neirong2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return list;
    }
    public boolean name1(String name) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "select Tnum from teacher where Tnum = '" + name + "'";
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return flag;
    }


    public List<Tbean> search1(String zi, String date) throws SQLException, ClassNotFoundException {
        String sql = "select * from teacher where ";


        if (zi != "") {
            sql += "Tnum like '%" +zi+ "%'";
        }



        List<Tbean> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Tbean bean = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String title2 = rs.getString("Tnum");
                String zi2 = rs.getString("Tname");
                String person2 = rs.getString("Tsex");
                String date2=rs.getString("course");

                bean = new Tbean(title2, zi2 ,person2,date2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return list;
    }

    public List<Tbean> list1() throws SQLException, ClassNotFoundException {
        String sql = "select * from teacher";
        List<Tbean> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                Tbean bean = null;

                String title2 = rs.getString("Tnum");
                String zi2 = rs.getString("Tname");
                String person2 = rs.getString("Tsex");
                String date2=rs.getString("nianji");


                bean = new Tbean(title2, zi2 ,person2,date2);

                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return list;
    }
}
复制代码

service.java

复制代码
package service;

import bean.Sbean;
import bean.Tbean;
import dao.Dao;

import java.sql.SQLException;
import java.util.List;

public class Service {
  Dao tDao=new Dao();
    public boolean add(Sbean ten) throws SQLException, ClassNotFoundException {
        boolean f = false;
        if(!tDao.name(ten.getNum()))
        {
            tDao.add(ten);
            f=true;
        }
        return f;
    }
    public boolean add1(Tbean ten) throws SQLException, ClassNotFoundException {
        boolean f = false;
        if(!tDao.name(ten.getTnum()))
        {
            tDao.add1(ten);
            f=true;
        }
        return f;
    }
    public boolean delete(String title) throws SQLException, ClassNotFoundException {
        tDao.delete(title);
        return true;
    }
    public boolean delete1(String title) throws SQLException, ClassNotFoundException {
        tDao.delete(title);
        return true;
    }
    public boolean update(Sbean ten)
    {
        tDao.update(ten);
        return true;
    }

    public Sbean getbytitle(String title) throws SQLException, ClassNotFoundException {
        return tDao.getbytitle(title);
    }

    public List<Sbean> search(String zi, String date) throws SQLException, ClassNotFoundException {
        return tDao.search(zi,date);
    }


    public List<Sbean> list() throws SQLException, ClassNotFoundException {
        return tDao.list();
    }
    public boolean update1(Tbean ten)
    {
        tDao.update1(ten);
        return true;
    }

    public Tbean getbytitle1(String title) throws SQLException, ClassNotFoundException {
        return tDao.getbytitle1(title);
    }

    public List<Tbean> search1(String zi, String date) throws SQLException, ClassNotFoundException {
        return tDao.search1(zi,date);
    }


    public List<Tbean> list1() throws SQLException, ClassNotFoundException {
        return tDao.list1();
    }
}