简体   繁体   English

用Java连接到MySQL数据库

[英]Connecting to MySQL database in Java

Can anyone help me out with this error here? 有人可以在这里帮助我解决此错误吗? Is it a syntax error? 这是语法错误吗?

Username:root No password 用户名:root没有密码

Code: 码:

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

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

Output: 输出:

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

You missed ? 你错过了? before user . user之前。

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

It's worth reading Oracle Tutorial - Establishing a Connection 值得阅读Oracle教程-建立连接


You can try with Properties as well. 您也可以尝试使用Properties Look at DriverManager.getConnection(String,Properties) constructor and there are more try with any one. 查看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);

You can use the following method signature instead 您可以改用以下方法签名

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

In your case it will be 在你的情况下

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

You're doing it wrong Try this 您做错了尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM