当前位置:网站首页>Dao and encapsulation of entity classes

Dao and encapsulation of entity classes

2022-06-21 18:49:00 LvhaoIT

Dao And entity class encapsulation

One 、jdbc Tool class encapsulation

package JDBC.Dao;

import java.sql.*;

public class JdbcUtil {
    
    private Connection conn = null;
    private PreparedStatement ps = null;

    static {
    
        try {
    
            // Registration drive 
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
    
            e.printStackTrace();
        }
    }

    // encapsulation conn objects creating 
    public Connection createCon() {
    
        try {
    
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/csdb?serverTimezone=UTC", "root", "lh051920");
            System.out.println("conn Object created successfully !");
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
            System.out.println("conn Object creation failed !");
        }
        return conn;
    }

    // encapsulation PreparedStatement object 
    public PreparedStatement createStatement(String sql) {
    
        Connection conn = createCon();
        try {
    
            ps = conn.prepareStatement(sql);
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        }
        return ps;
    }

    // Package destruction 
    public void close(ResultSet rs) {
    
        try {
    
            if (rs != null)
                rs.close();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        }
    }

    public void close() {
    
        try {
    
            if (ps != null)
                ps.close();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        }
        try {
    
            if (conn != null)
                conn.close();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        }

    }

}

Two 、DeptDao Encapsulation

package JDBC.Dao;

import JDBC.entity.Dept;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/** *  encapsulation dept The operation of the table  */
public class DeptDao {
    
    private JdbcUtil util = new JdbcUtil();

    public int add(String deptno, String dname, String loc) {
    
        String sql = "insert into dept (deptno,dname,loc) values(?,?,?)";
        PreparedStatement ps = this.util.createStatement(sql);
        int result = 0;
        try {
    
            ps.setInt(1, Integer.valueOf(deptno));
            ps.setString(2, dname);
            ps.setString(3, loc);
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        } finally {
    
            util.close();
        }
        return result;
    }

    public int delete(String deptno) {
    
        String sql = "delete from dept where deptno=?";
        PreparedStatement ps = this.util.createStatement(sql);
        int result = 0;
        try {
    
            ps.setInt(1, Integer.valueOf(deptno));
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        } finally {
    
            util.close();
        }
        return result;
    }

    public int update(String deptno, String dname, String loc) {
    
        String sql = "update dept set dname=?,loc=? where deptno=?";
        PreparedStatement ps = this.util.createStatement(sql);
        int result = 0;
        try {
    
            ps.setString(1, dname);
            ps.setString(2, loc);
            ps.setInt(3, Integer.valueOf(deptno));
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        } finally {
    
            util.close();
        }
        return result;
    }

    public List select() {
    
        List list = new ArrayList();
        String sql = "select * from dept";
        ResultSet rs = null;
        PreparedStatement ps = this.util.createStatement(sql);
        try {
    
            rs = ps.executeQuery();
            while (rs.next()) {
    
                int deptno = rs.getInt("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                list.add(new Dept(deptno, dname, loc));
            }
        } catch (SQLException throwables) {
    
            throwables.printStackTrace();
        } finally {
    
            util.close(rs);
        }
        return list;
    }


}

3、 ... and 、Dept Data table encapsulation

package JDBC.entity;

// Table entity 
public class Dept {
    
    private int deptno;
    private String dname;
    private String loc;

    public Dept(int deptno, String dname, String loc) {
    
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public Dept() {
    
    }

    public int getDeptno() {
    
        return deptno;
    }

    public void setDeptno(int deptno) {
    
        this.deptno = deptno;
    }

    public String getDname() {
    
        return dname;
    }

    public void setDname(String dname) {
    
        this.dname = dname;
    }

    public String getLoc() {
    
        return loc;
    }

    public void setLoc(String loc) {
    
        this.loc = loc;
    }


}

Four 、 Test code

package JDBC.Test;

import JDBC.Dao.DeptDao;
import JDBC.entity.Dept;

import java.util.List;
import java.util.Scanner;

public class  Department of management  {
    
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
    
        menu();

    }

    private static void menu() {
    
        int flag;
        String deptno = null;
        String dname = null;
        String loc = null;
        DeptDao dept = new DeptDao();
        System.out.println("===== Department management system =====");
        System.out.println("===1. Add Department ========");
        System.out.println("===2. Delete Department ========");
        System.out.println("===3. Update department ========");
        System.out.println("===4. Check all departments =====");
        System.out.println("===0. Exit the system ========");
        System.out.print(" Please select the function :");
        flag = in.nextInt();
        in.nextLine();
        int result = 0;
        switch (flag) {
    
            case 1:
                System.out.print(" Please enter the department number :");
                deptno = in.nextLine();
                System.out.print(" Please enter the Department name :");
                dname = in.nextLine();
                System.out.print(" Please enter the address :");
                loc = in.nextLine();
                result = dept.add(deptno, dname, loc);
                if (result == 1)
                    System.out.println(" Department added successfully !");
                else
                    System.out.println(" Failed to add Department !");
                break;
            case 2:
                System.out.print(" Please enter the department number :");
                deptno = in.nextLine();
                result = dept.delete(deptno);
                if (result == 1)
                    System.out.println(" Delete Department succeeded !");
                else
                    System.out.println(" Failed to delete Department !");
                break;
            case 3:
                System.out.print(" Please enter the department number :");
                deptno = in.nextLine();
                System.out.print(" Please enter the Department name :");
                dname = in.nextLine();
                System.out.print(" Please enter the address :");
                loc = in.nextLine();
                result = dept.update(deptno, dname, loc);
                if (result == 1)
                    System.out.println(" Department modified successfully !");
                else
                    System.out.println(" Failed to modify Department !");
                break;
            case 4:
                System.out.println(" Department number \t\t Department name \t\t Department Address ");
                List<Dept> list = dept.select();
                for (Dept d : list) {
    
                    System.out.println(d.getDeptno() + "\t\t" + d.getDname() + "\t\t" + d.getLoc());
                }
            case 0:
                break;
        }


    }
}

5、 ... and 、 Running results

===== Department management system =====
===1. Add Department ========
===2. Delete Department ========
===3. Update department ========
===4. Check all departments =====
===0. Exit the system ========
  
 Please select the function :4
 Department number 	 Department name 		 Department Address 
10		ACCOUNTING		NEW YORK
20		RESEARCH		DALLAS
30		SALES			CHICAGO
40		OPERATIONS		BOSTON
60		 engineering department 				 xuzhou 
    
原网站

版权声明
本文为[LvhaoIT]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211712399735.html