IDEA JDBC配置

一、在pom中添加依赖

html 复制代码
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>

然后同步一下

二、编写代码

java 复制代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        Connection connection = null;
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "root";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            System.out.println("Exception." + e.getMessage());
        } finally {
            // Close the connection
            if (connection != null) {
                try {
                    System.out.println("Success to  connection.");
                    connection.close();
                } catch (SQLException e) {
                    System.out.println("Failed to close connection.");
                    e.printStackTrace();
                }
            } else {
                System.out.println("Failed to  connection.");
            }
        }
    }
}

这是我的数据库名

相关推荐
还是鼠鼠1 小时前
AI掘金头条新闻系统 (Toutiao News)-用户注册-生成Token
后端·python·mysql·fastapi·web
TechWayfarer8 小时前
查询IP所在地的3种方案:从API到离线库,风控场景怎么选?
开发语言·网络·python·网络协议·tcp/ip
程序员榴莲9 小时前
Python 单例模式
开发语言·python·单例模式
hh.h.9 小时前
昇腾CANN ops-transformer 仓的 MC2 算子:MoE 模型的全到全通信
python·深度学习·transformer·cann
莞凰9 小时前
昇腾CANN的“灵脉根基“:Runtime仓库探秘
android·人工智能·transformer
NiceCloud喜云10 小时前
Claude Files API 深入:从上传、复用到配额管理的工程化指南
android·java·数据库·人工智能·python·json·飞书
ujainu10 小时前
CANN pto-isa:虚拟指令集如何连接编译与执行
android·ascend
专注VB编程开发20年11 小时前
windows下python自带标准库 ≈ 70% 纯.py 源码,30% .pyd(DLL)
python
萌新小码农‍11 小时前
人工智能数学基础+python实例(人工智能学习day3)
开发语言·人工智能·python