IDEA JDBC配置

一、在pom中添加依赖

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>

然后同步一下

二、编写代码

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. Git<span style='color:red;'>配置</span>

    Git配置

    2024-02-03 07:18:01      41 阅读
  2. Redisson配置

    2024-02-03 07:18:01       40 阅读
  3. nginx配置

    2024-02-03 07:18:01       38 阅读
  4. VLAN配置

    2024-02-03 07:18:01       30 阅读
  5. XMLParser配置

    2024-02-03 07:18:01       38 阅读
  6. Redis 配置

    2024-02-03 07:18:01       29 阅读
  7. NAT<span style='color:red;'>配置</span>

    NAT配置

    2024-02-03 07:18:01      35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 07:18:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 07:18:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 07:18:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 07:18:01       18 阅读

热门阅读

  1. PyQt子线程处理业务事件

    2024-02-03 07:18:01       30 阅读
  2. Ubuntu 安装 Docker 详细步骤

    2024-02-03 07:18:01       32 阅读
  3. nuxt.js中使用axios以及二次封装

    2024-02-03 07:18:01       28 阅读
  4. WPF简介

    WPF简介

    2024-02-03 07:18:01      29 阅读
  5. LED显示屏在XR虚拟拍摄中的应用及前景

    2024-02-03 07:18:01       28 阅读
  6. 数据聚类:一种有效的数据分析技术

    2024-02-03 07:18:01       32 阅读
  7. 【gcc】webrtc发送侧 基于丢包更新码率

    2024-02-03 07:18:01       35 阅读
  8. uniapp实现自定义底部tab栏

    2024-02-03 07:18:01       30 阅读
  9. QT中的QImage与QPixmap区别

    2024-02-03 07:18:01       29 阅读
  10. webpack详解

    2024-02-03 07:18:01       27 阅读