• 关于增删改查的例子


    通过前面的练习发现自己对于增删改查的知识仍然不够熟练,今天进行一个基于课程管理的增删改查功能的实现。

    Dbutil:

    复制代码
    复制代码
    复制代码
    package com.zh.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.SQLException;
    
    public class DButil {
        public static Connection getConnection(){
            Connection conn = null;
            try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:mysql://localhost:3306/teacher_class?serverTimezone=UTC";
            conn=DriverManager.getConnection(url, "root", "hao19990507.");
            }catch (Exception e){
                e.printStackTrace();
            }
            return conn;
        }
        
        public static void close(Statement sta,Connection con){
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void close(ResultSet rs,Statement sta,Connection con){
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    复制代码
    复制代码
    复制代码

    bean:

    复制代码
    复制代码
    复制代码
    package com.zh.entity;
    
    
    
        public class bean {
        private String place;
        private String teacher_name;
        private String class_name;
        public String get_place() {
            return place;
        }
        public void set_place(String place) {
            this.place=place;
        }
        public String get_teacher_name() {
            return teacher_name;
        }
        public void set_teacher_name(String teacher_name) {
            this.teacher_name=teacher_name;
        }
        public String get_class_name() {
            return class_name;
        }
        public void set_class_name(String class_name) {
            this.class_name=class_name;
        }
        public bean() {};
        
        public bean(String place,String teacher_name,String class_name) {
            this.place=place;
            this.teacher_name=teacher_name;
            this.class_name=class_name;
            }
    }
    复制代码
    复制代码
    复制代码

    data_dao:

    复制代码
    复制代码
    复制代码
    package com.zh.dao;
    import java.util.*;
    import java.sql.*;
    import com.zh.entity.*;
    import com.zh.util.*;
    import java.sql.*;
    public class data_dao {
        /*增加
         * 
         */
    public void add(bean Bean) {
        String sql="insert into class_message values('"+Bean.get_place()+"','"+Bean.get_teacher_name()+"','"+Bean.get_class_name()+"')";
        Connection conn = DButil.getConnection();
        Statement state = null;
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    /*删除
     * 
     */
    public void deleteByteacher_name(String teacher_name) {
        boolean f=false;
        String sql="delete from class_message where teacher_name='"+teacher_name+"'";
        Connection conn=DButil.getConnection();
        Statement state = null;
        
        try {
            
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        
        }
             /* 修改
             * 
             */
    
    public void updata(bean b) {
        String sql="updata class_message set place='"+b.get_place()+"', teacher_name='"+b.get_teacher_name()+"',class_name='"+b.get_class_name()+"' where teacher_name='"+b.get_teacher_name()+"'";
        
        Connection conn=DButil.getConnection();
        Statement state = null;
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        
    
    }
    /**
     * 验证课程名称是否唯一
     */
    public List<bean> isOnly_class_name(String class_name) {
        List list=new ArrayList<bean>();
        String sql="select name from class_message where class_name='"+class_name+"'";
        Connection conn=DButil.getConnection();
        Statement state=null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while(rs.next()) {
                String class_name1=rs.getString("class_name");
                String teacher_name=rs.getString("teacher_name");
                String place=rs.getString("place");
                bean b=new bean(place,teacher_name,class_name1);
                list.add(b);
            }
        } catch (SQLException e) {
        e.printStackTrace();
    }
        return list;
    }
    
                                                                                                                                                                                                                                                                           
    /**
     * 通过teacher_name获得一个bean对象
     */
    public bean findByteacher_name(String teacher_name) {
            Connection conn=null;
        Statement state=null;
        
        try {
            conn=DButil.getConnection();
            String sql="select*from class_message where teacher_name='"+teacher_name+"'";
            
            state=conn.createStatement();
            ResultSet rs=state.executeQuery(sql);
            bean u=new bean();;
            while(rs.next()) {
                String t_name=rs.getString("teacher_name");
                String c_name=rs.getString("class_name");
                String place=rs.getString("place");
                u.set_class_name(c_name);
                u.set_place(place);
                u.set_teacher_name(t_name);
                return u;
            }
        }catch (Exception e) {
            e.printStackTrace();
            }
    
    return null;
    }
    /*
     * 显示全部
     */
    public List<bean> findAll(){
        Connection conn=DButil.getConnection();
        List<bean> list=new ArrayList<bean>();
        bean b=null;
        try {
        Statement sta=conn.createStatement();
        String sql="select * from class_message";
        ResultSet rs=sta.executeQuery(sql);
            while(rs.next()) {
                b=new bean();
                String t_name,c_name,place;
                t_name=rs.getString("teacher_name");
                c_name=rs.getString("c_name");
                place=rs.getString("place");
                b.set_class_name(c_name);
                b.set_place(place);
                b.set_teacher_name(t_name);
                list.add(b);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        for(int i=0;i<list.size();i++) {
            bean br=(bean)list.get(i);
            System.out.println(br.get_teacher_name());
        }
        return list;
    }
    }
    复制代码
    复制代码
    复制代码

    Userservice:

    复制代码
    复制代码
    复制代码
    package com.zh.service;
    import java.util.*;
    import com.zh.dao.*;
    import com.zh.entity.*;
    public class Userservice {
    public boolean insertUser(String place,String teacher_name,String class_name) {
        data_dao dao=new data_dao();
        bean b=new bean();
        b.set_class_name(class_name);
        b.set_place(place);
        b.set_teacher_name(teacher_name);
        try {
        dao.add(b);
        return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean deleteUser(String teacher_name) {
        data_dao dao=new data_dao();
        bean b=dao.findByteacher_name(teacher_name);
        if(b!=null) {
            dao.deleteByteacher_name(teacher_name);
            return true;
        }else 
            return false;
    }
    public boolean updataByteacher_name(String place,String class_name,String teacher_name) {
        data_dao dao=new data_dao();
        bean b=new bean(place,class_name,teacher_name);
        try {
            dao.updata(b);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        }
    public boolean isOnly_classname(String class_name) {
        data_dao dao=new data_dao();
        try {
            List list=dao.isOnly_class_name(class_name);
            if(list.size()>1)
                return true;
            else
                return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public List<bean> findAll_user(){
        data_dao dao=new data_dao();
        List list=dao.findAll();
        return list;
    }
    }
  • 相关阅读:
    ado.net(增删改)
    窗体基础WINFORM
    SQL函数类的操作,增加,查询
    SQL数据库,增加查询修改以及防sql写入攻击
    SQL数据库--数据访问
    单列模式
    SQL数据库基础————委托
    SQL数据库基础知识——抽象类
    SQL数据库——静态成员
    面向对象
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14916674.html
Copyright © 2020-2023  润新知