无法通过JDBC与MySQL数据库连接

发布于 2025-02-09 05:51:27 字数 1189 浏览 0 评论 0原文

我试图通过JDBC与MySQL数据库进行连接。但是我无法将其与VS代码联系起来。我只是一个问题,我们需要Mavan项目总是与DB连接。我还添加了代码,请参考并建议我如何与DB连接。 谢谢!

我连接时遇到的错误。 在此处输入图像描述

package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;

public class createDb {
    // JDBC and database properties.
    private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "root";
    
public static void main(String args[]) {
        Connection conn = null;
        try {
            // Register the JDBC driver
            Class.forName(DB_DRIVER);
            // Open the connection
            conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            if (conn != null) {
                System.out.println("Successfully connected.");
            } else {
                System.out.println("Failed to connect.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I have tried to connect with the mysql database by the JDBC. But I am no able to connect it with the vs code.I just have a question that we need the mavan project to connect with db always. I have also add the code please Refer and suggest me how can i connect with the db.
Thanks!

The error that i have got while connecting.
enter image description here

package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;

public class createDb {
    // JDBC and database properties.
    private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "root";
    
public static void main(String args[]) {
        Connection conn = null;
        try {
            // Register the JDBC driver
            Class.forName(DB_DRIVER);
            // Open the connection
            conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            if (conn != null) {
                System.out.println("Successfully connected.");
            } else {
                System.out.println("Failed to connect.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文