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 天前
Python自学12 — 函数和模块
开发语言·python
咖啡の猫1 天前
Android开发-常用布局
android·gitee
THMAIL1 天前
深度学习从入门到精通 - 迁移学习实战:用预训练模型解决小样本难题
人工智能·python·深度学习·算法·机器学习·迁移学习
程序员老刘1 天前
Google突然“变脸“,2026年要给全球开发者上“紧箍咒“?
android·flutter·客户端
和小胖11221 天前
第一讲 Vscode+Python+anaconda 安装
python
Tans51 天前
Androidx Lifecycle 源码阅读笔记
android·android jetpack·源码阅读
和小胖11221 天前
第二讲 Vscode+Python+anaconda 高阶环境配置
ide·vscode·python
雨白1 天前
实现双向滑动的 ScalableImageView(下)
android
峥嵘life1 天前
Android Studio新版本编译release版本apk实现
android·ide·android studio
小胖墩有点瘦1 天前
【基于yolo和web的垃圾分类系统】
人工智能·python·yolo·flask·毕业设计·课程设计·垃圾分类