mysql与idea连接

1、安装mysql,确保电脑中有sql数据库;

2、在'服务'中开启mysql;

3、将mysql-connector-java-8.0.16.jar包放入web/WEB-INF/lib并配置;

  • mysql-connector-java 5及以下,配置的是com.mysql.jdbc.Driver驱动
  • mysql-connector-java 6及以上,配置的是com.mysql.cj.jdbc.Driver驱动

4、确认个人sql所用端口,用户名,密码正确。

java 复制代码
//建立连接

public class DBUtil {
    private static Connection conn = null;

    public static Connection getConnection() {
        String url =
                "jdbc:mysql://localhost:3306(个人端口)/mydb?serverTimezone=UTC&useSSL=false";
//与创建数据库的代码结合,保证访问数据库时不出现乱码
        String username = "root(个人用户名)";
        String password = "******(个人密码)";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");// 加载数据库驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (conn == null)
            try {
                conn = DriverManager.getConnection(url, username, password);//建立连接
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.print("数据库连接失败!");
            }
        return conn;
    }
}

end~

相关推荐
TG:@yunlaoda360 云老大19 小时前
如何在华为云国际站代理商控制台进行SFS Turbo的性能与容量核查?
服务器·网络·数据库·华为云
ytttr87320 小时前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
云老大TG:@yunlaoda36020 小时前
如何进行华为云国际站代理商跨Region适配?
大数据·数据库·华为云·负载均衡
思成不止于此21 小时前
【MySQL 零基础入门】事务精讲(二):ACID 特性与并发问题
数据库·笔记·学习·mysql
Boilermaker199221 小时前
[MySQL] 初识 MySQL 与 SQL 基础
数据库·mysql
今晚务必早点睡21 小时前
Redis——快速入门第二课:Redis 常用命令 + 能解决实际问题
数据库·redis·bootstrap
Hello.Reader1 天前
Flink SQL Materialized Table 语句CREATE / ALTER / DROP介绍
数据库·sql·flink
Boilermaker19921 天前
[MySQL] 服务器架构
数据库·mysql·架构
云老大TG:@yunlaoda3601 天前
如何通过华为云国际站代理商CSBS进行备份策略设置?
运维·数据库·华为云
一 乐1 天前
酒店预约|基于springboot + vue酒店预约系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端