Using the idea of encapsulation to write the addition, deletion, query and modification of MySQL

Keywords: SQL Java Database MySQL

The items involved in this experiment are as follows:

It mainly involves four java files:
Test.java
Userinfo.java
Userinfodao.java
DBUtil.java

Let's explain one by one:
The Userinfo code is as follows: this part is mainly entity class, corresponding to database.

package com.zhongruan.util.bean;

public class Userinfo {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Userinfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

The code of Userinfodao.java is as follows: this part is the persistence layer, which stores the functions of adding, deleting, querying and modifying database.

package com.zhongruan.util.dao;

import com.zhongruan.util.DBUtil;
import com.zhongruan.util.bean.Userinfo;

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

public class Userinfodao {
    public List<Userinfo> findAll(){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<Userinfo> list=new ArrayList<>();
        try {
            connection= DBUtil.getconnection();
            //3. write sql
            String sql = "select* from userinfo";
            //4. Get the statement object
            statement = connection.prepareStatement(sql);
            //5. Execute sql to get result set
            rs = statement.executeQuery();
            //6. Processing result set
            while (rs.next()) {
                Userinfo userinfo=new Userinfo();
                userinfo.setId(rs.getInt(1));
                userinfo.setUsername(rs.getString(2));
                userinfo.setPassword(rs.getString(3));
                list.add(userinfo);
            }

            /* 7.close resource */
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,rs);
        }
        return list;
    }

    public void add(){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection= DBUtil.getconnection();
            //3. write sql
            String sql = "insert into userinfo (username,password) values(?,?)";
            //4. Get the statement object
            statement = connection.prepareStatement(sql);
            statement.setString(1,"tianhe");
            statement.setString(2,"666");
            statement.executeUpdate();

            /* 7.close resource */
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }


    public void delete(){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection= DBUtil.getconnection();
            //3. write sql
            String sql = "delete from userinfo where username='Wang Ao'";
            //4. Get the statement object
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();

            /* 7.close resource */
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }


    public void update(){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection= DBUtil.getconnection();
            //3. write sql
            String sql = "update userinfo set password=555 where id=3";
            //4. Get the statement object
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();

            /* 7.close resource */
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }
}

DBUtil.java is the tool part, which is mainly used to connect and close the database. Code:

package com.zhongruan.util;

import java.sql.*;

public class DBUtil {
public static Connection getconnection(){
    Connection connection = null;
    //1. Loading drive
    try {
        Class.forName("com.mysql.jdbc.Driver");
        //2. Create a connection
        connection = DriverManager.getConnection
                ("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" +
                        "characterEncoding=utf-8&user=" +
                        "root&password=123");
        System.out.println("Connection created successfully");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connection;
}

public static void close(Connection connection,PreparedStatement statement,ResultSet rs) {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
}

Test.java is the class of the main function of the whole operation. It calls methods in the other three classes to add, delete, query and modify:

ackage com;

import com.zhongruan.util.dao.Userinfodao;

public class Test {
    public static void main(String[] args) {
        Userinfodao userinfodao=new Userinfodao();
        //List<Userinfo> list=userinfodao.findAll();
        //userinfodao.add();
        //userinfodao.delete();
        userinfodao.update();
    }
    }

Take the modification as an example to show the running results:
Table information of the original database:

Now change the password of the user with id=3 to 555

Function:

View the database table information. Modification succeeded:

Posted by NTM on Wed, 30 Oct 2019 08:08:22 -0700