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

这是我的数据库名

相关推荐
CS_Zero6 分钟前
安卓开发:Linux的android工具命令
android·linux·运维
fanruitian18 分钟前
python 机器学习 交叉验证train_test_split 网格搜索GridSearchCV
开发语言·python·机器学习
fellow9931 分钟前
DSH Desktop - Android Edition
android·agent·deepseek·harness·dsh
Carson带你学Android36 分钟前
Android Studio Rabbit 2 发布:除了Gemini,40+ AI Agent 正式入驻!
android·ai编程
前端精髓10 小时前
结构化输出with_structured_output(抽卡)
python·langchain
尹人入圣11 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
开开心心_Every11 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
Nuanyt11 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness
吴卫斌11 小时前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
2601_9679873712 小时前
Python——入门级(定义功能def函数)
python·函数·可变参数·默认参数·关键字参数