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

这是我的数据库名

相关推荐
木易士心3 分钟前
Android Handler 机制原理详解
android·app
用户20187928316710 分钟前
CoroutineDispatcher的"自由精灵" - Dispatchers.Unconfined
android
用户20187928316710 分钟前
用 “奶茶连锁店的部门分工” 理解各种 CoroutineScope
android
gc_229916 分钟前
学习Python中Selenium模块的基本用法(19:操作下拉框)
python·selenium
我的xiaodoujiao25 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 19--测试框架Pytest基础 3--前后置操作应用
python·学习·测试工具·pytest
计算衎34 分钟前
基于Python实现CANoe和UDE交互通信工具实现,CAPL脚本通过python交互工具与UDE进行通信和调用UDE的组件获取UDE返回值。
python·capl·canoe·ude·nm_oncan
报错小能手35 分钟前
python(入门)map内置函数及import模块导入,as别名
开发语言·人工智能·python
黄额很兰寿43 分钟前
深入源码理解LiveData的实现原理
android
黄额很兰寿1 小时前
flow 的冷流和热流 是设么有什么区别?
android