簡體   English   中英

將 MySQL 數據庫連接到 NetBeans 錯誤

[英]Connecting MySQL Database to NetBeans Error

在 NetBeans 中運行我的代碼以查看 mySQL 是否已連接時遇到問題。 這是代碼:

public static void main(String[] args) {
    Connection connect = null;

    try{
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
        if(connect!=null)
        {
        System.out.println("Connected");
        }
    }catch (Exception e)
    {
        System.out.println("RIP");
    }
  }
}

當我運行它時會打印出“RIP”。 當我一行一行地調試它時,它從“connect = DriverManager.getConnection...”到“System.out.println(“RIP”),當我查看“Exception e”時,它說“e = (java.sql.SQLNonTransientConnectionException) java.sql.SQLNonTransientConnectionException:無法加載連接類,因為底層異常:com.mysql.cj.exceptions.WrongArgumentException:格式錯誤的數據庫 URL,無法解析 '=TRUE' 附近的連接字符串。”

現在,這是為什么????

我認為你需要添加Class.forName("com.mysql.jdbc.Driver"); .

另外,請確保Connection conn = DriverManager.getConnection (String url, String user, String password); 設置正確。

從代碼中的 url 格式來看,就像您試圖直接連接到數據庫中的特定表tUsers 我不認為那會奏效。 如果我錯了,請糾正我,無論它是否是您的數據庫名稱。

因為我知道的基本url 格式應該像jdbc:mysql://localhost:3306/yourDBname

如果您已經按照帖子中所寫的方式正確設置了 url,則代碼為

public static void main(String[] args) {

try{
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/tUsers?autoReconnect=true/useSSL=TRUE","root","password");
    if(connect!=null)
    {
    System.out.println("Connected");
    }
}catch (Exception e)
{
    System.out.println("RIP");
}}}

希望可以完成這項工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM