繁体   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