2.28号今日总结

今天我弄了一些增删改查的代码,下面是我的总结,主要的增删改查是在dao层

复制代码
package dao;

import Util.DBUtil;
import bean.Sbean;
import bean.Cbean;

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(CardId,StuName,sex,College,Professionals,Phone,banji)values"
                + "('" + ten.getStuID() + "','" + ten.getStuName() + "','"+ ten.getSex() + "','" + ten.getCollege() + "','" + ten.getProfessionals() + "','" + ten.getPhone() + "','" + ten.getBanji() +  "')";

        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 add(Cbean ten) throws ClassNotFoundException , SQLException
    {
        String sql="insert into student(CardId,CourseName,CourseTeacher,CourseID,CourseNature,Credit,CourseClass,CourseMajor,TestWay,TestData,TestCount,TestMethod,TestGrade,TestEvaluation,TestAnalysis)values"
                + "('" + ten.getCardId() + "','" + ten.getCourseName() + "','"+ ten.getCourseTeacher() + "','" + ten.getCourseID() + "','" + ten.getCourseNature() + "','" + ten.getCredit() + "','" + ten.getCourseClass() +"','" + ten.getCourseMajor() + "','" + ten.getTestWay() + "','" + ten.getTestData() + "','" + ten.getTestCount() +"','" + ten.getTestMethod() + "','" + ten.getTestGrade() + "','" + ten.getTestEvaluation() + "','" + ten.getTestAnalysis() +  "')";

        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 Sbean getbyzhuti(String name) throws ClassNotFoundException ,SQLException
    {
        String sql = "select * from shetuan where zhuti ='" + 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()) {
                int id=rs.getInt("id");

                String zhuti2 = rs.getString("zhuti");
                String mudi2 = rs.getString("mudi");
                String leixing2 = rs.getString("leixing");
                String time2=rs.getString("time");
                String place2 = rs.getString("place");
                String duixiang2 =rs.getString("duixiang");
                String neirong2=rs.getString("neirong");
                String anpai2=rs.getString("anpai");

               // ten = new Sbean(id,zhuti2, mudi2,leixing2,time2,place2, duixiang2, neirong2, anpai2);
            }
        } 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 testcard where CardId='" + 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 update(Sbean b)
    {
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            String sql="update shetuan set zhuti=?,mudi=?,leixing=?,time=?,place=?,duixiang=?,neirong=?,anpai=? where id=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1, b.getZhuti());
            pstmt.setString(2, b.getMudi());
            pstmt.setString(3, b.getLeixing());
            pstmt.setString(4, b.getTime());
            pstmt.setString(5, b.getPlace());
            pstmt.setString(6, b.getDuixiang());
            pstmt.setString(7, b.getNeirong());
            pstmt.setString(7, b.getAnpai());
            pstmt.setInt(8, b.getId());
            pstmt.executeUpdate();
            return true;
        }
        catch (SQLException | ClassNotFoundException e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }
        finally {
            DBUtil.close(rs, pstmt, con);
        }
        return false;
    }*/
    //查找
    public boolean name(String name) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "select zhuti from shetuan where zhuti = '" + 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 zhuti, String time, String leixing, String place) throws SQLException, ClassNotFoundException {
        String sql = "select * from shetuan where ";


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

        if (time != "") {
            sql += "time like '%" +time+ "%'";
        }
        if(leixing!="") {
            sql+="leixing like '%"+leixing+ "%'";
        }

        if(place!="") {
            sql+="place like '%" +place+ "%'";
        }
        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 zhuti2 = rs.getString("zhuti");
                String mudi2 = rs.getString("mudi");
                String leixing2 = rs.getString("leixing");
                String time2=rs.getString("time");
                String place2 = rs.getString("place");
                String duixiang2 =rs.getString("duixiang");
                String neirong2=rs.getString("neirong");
                String anpai2=rs.getString("anpai");

               // bean = new Sbean(zhuti2, mudi2,leixing2,time2,place2, duixiang2, neirong2, anpai2);

                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 shetuan";
        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;
                int id=rs.getInt("id");
                String zhuti2 = rs.getString("zhuti");
                String mudi2 = rs.getString("mudi");
                String leixing2 = rs.getString("leixing");
                String time2=rs.getString("time");
                String place2 = rs.getString("place");
                String duixiang2 =rs.getString("duixiang");
                String neirong2=rs.getString("neirong");
                String anpai2=rs.getString("anpai");

               // bean = new Sbean(id,zhuti2, mudi2,leixing2,time2,place2, duixiang2, neirong2, anpai2);

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

        return list;
    }
}
复制代码

 

posted @   庞司令  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示