每日分享 – 【学习笔记】JDBC MySQL8.0连接 – Java

/**
 * Title
 *
 * @ClassName: DataBaseConnection
 * @Description: 数据库连接类
 * @author: Karos
 * @date: 2022/5/16 11:11
 * @Blog: https://www.wzl1.top/
 */

package com.jdbc;

import com.table.USERS;
import sun.management.LockInfoCompositeData;

import java.sql.*;
import java.util.concurrent.locks.ReentrantLock;

public class DataBaseConnection {
    private static DataBaseConnection Single=null;
    private String mysqlAccount = "root";
    private String mysqlPassWord = "wzl20030211";
    private String connect_URL = "jdbc:mysql://localhost:3306/_admins?useUnicode=true&character=utf8&useSSL=false&serverTimezone=UTC";
    private Connection connection=null;
    /**mysql驱动*/
    {
        try {
           Class.forName("com.mysql.jc.jdbc.Driver");
            System.out.println("驱动加载成功!");
        } catch (ClassNotFoundException e) {
            System.err.println("驱动加载失败!");
            e.printStackTrace();
        }
    }

    /**
     * 构造器,连接数据库
     */
    private DataBaseConnection(){
        try {
            this.connection=DriverManager.getConnection(connect_URL,mysqlAccount,mysqlPassWord);
            System.out.println("连接成功");
        } catch (SQLException e) {
            System.err.println("连接失败");
            e.printStackTrace();
        }
    }
    public void insert(USERS user){
        String str="insert admins value (?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps=connection.prepareStatement(str);
            ps.setInt(1,user.getId());
            ps.setString(2,user.getName());
            ps.setString(3,user.getAccount());
            ps.setString(4,user.getPassword());
            ps.setString(5,user.getSex());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void Update(USERS user){
        String str="update admins set name=?,account=?,password=?,sex=? where id=?";
        PreparedStatement ps = null;
        try {
            ps=connection.prepareStatement(str);
            ps.setString(1,user.getName());
            ps.setString(2,user.getAccount());
            ps.setString(3,user.getPassword());
            ps.setString(4,user.getSex());
            ps.setInt(5,user.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private static ReentrantLock Lock= new ReentrantLock(false);

    /**
     * 连接数据库
     * @return
     */
    public static DataBaseConnection LINK(){
        Lock.lock();
        if(Single==null){
            Single = new DataBaseConnection();
        }
        Lock.unlock();
        return Single;
    }
}

USER类

/**
 * Title
 *
 * @ClassName: USERS
 * @Description:
 * @author: Karos
 * @date: 2022/5/16 11:31
 * @Blog: https://www.wzl1.top/
 */

package com.table;

public class USERS {
    private int id;
    private String name;
    private String account;
    private String password;
    private String sex;

    public USERS(int id, String name, String account, String password, String sex) {
        this.id = id;
        this.name = name;
        this.account = account;
        this.password = password;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "USERS{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

Main入口函数

import com.jdbc.DataBaseConnection;
import com.table.USERS;

/**
 * Title
 *
 * @ClassName: Main
 * @Description:
 * @author: Karos
 * @date: 2022/5/16 11:05
 * @Blog: https://www.wzl1.top/
 */

public class Main {
    public static void main(String[] args) {
        DataBaseConnection SQL = DataBaseConnection.LINK();
        USERS user=new USERS(5,"小红","xiaohong","123456","女");
        SQL.Update(user);
    }
}

正文完