簡體   English   中英

用Java連接到MySQL數據庫

[英]Connecting to MySQL database in Java

有人可以在這里幫助我解決此錯誤嗎? 這是語法錯誤嗎?

用戶名:root沒有密碼

碼:

    Class.forName("com.mysql.jdbc.Driver");

    Connection conn = null;
    conn = DriverManager.getConnection("jdbc:mysql://localhost/main"
            + "user=root&password=");

輸出:

Access denied for user ''@'localhost' to database 'mainuser=root&password='

你錯過了? user之前。

DriverManager.getConnection("jdbc:mysql://localhost/main?" +
                               "user=root&password=");

值得閱讀Oracle教程-建立連接


您也可以嘗試使用Properties 查看DriverManager.getConnection(String,Properties)構造函數,可以嘗試任何一種。

Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "");

String url ="jdbc:mysql://localhost/main";

Connection conn = DriverManager.getConnection(url, connectionProps);

您可以改用以下方法簽名

public static Connection getConnection(String url, String user, String password) throws SQLException

在你的情況下

conn = DriverManager.getConnection("jdbc:mysql://localhost/main", "root", "");

您做錯了嘗試

(Jdbc:mysql://本地主機/ main,root,passwd)

暫無
暫無

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

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