前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【学习笔记】JDBC MySQL8.0连接 – Java

【学习笔记】JDBC MySQL8.0连接 – Java

作者头像
Karos
发布2023-01-03 16:12:43
3040
发布2023-01-03 16:12:43
举报
文章被收录于专栏:MyBlog-KarosMyBlog-Karos
代码语言:javascript
复制
/**
 * 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类

代码语言:javascript
复制
/**
 * 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入口函数

代码语言:javascript
复制
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);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-5-16 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档