无法通过Java应用程序连接到数据库mysql

时间:2018-12-18 11:24:00

标签: java mysql

这是连接mysql数据库的简单代码,用户名和密码正确。 我无法连接到mysql数据库。 有什么建议吗?

package provaconnessionemysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class connessionemysql {

    public static void main(String[] args) {

        System.out.println("inizio programma connessione mysql");

        try {Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registropeso/" + "user=andrea&password=password");
            System.out.println("connessione aperta");
            conn.close(); // termino la connessione
            System.out.println("connessione chiusa");
        }

        catch(ClassNotFoundException e) {
            System.out.println(e);
            System.out.println("LocalizedMessage: " + e.getLocalizedMessage());
            System.out.println("Message: " + e.getMessage());
            System.out.println("Hashcode: " + e.hashCode());
            System.out.println("Cause: " + e.getCause());

        }
        catch(SQLException e) {
            System.out.println(e);
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

getConnection()方法已经具有用户和密码参数。

getConnection(String url, String user, String password)

分别指定它。

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registropeso/","andrea","password");