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.");
            }
        }
    }
}

这是我的数据库名

相关推荐
柚鸥ASO优化14 小时前
安卓APP推广的“降本增效”密码:守好商店内,打好商店外
android·aso优化
Esaka_Forever14 小时前
Python 完整内存管理机制详解
开发语言·python·spring
我是一颗柠檬15 小时前
【Java项目技术亮点】EXPLAIN深度分析与慢查询治理
android·java·开发语言
Android-Flutter15 小时前
android compose shadow 阴影 使用
android·kotlin·compose
Weigang15 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源
小九九的爸爸15 小时前
前端入门Agent开发,掌握这些Python数据基础就够啦
python·agent
风之所往_15 小时前
Python 3.9 新特性全面总结
python
W是笔名15 小时前
python___容器类型的数据___序列
开发语言·python
aqi0015 小时前
15天学会AI应用开发(十一)从TXT文件构建RAG知识库
人工智能·python·大模型·ai编程·ai应用