且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JDBC问题连接到远程MySQL服务器

更新时间:2023-02-04 21:30:07

您的MySQL服务器是否可从服务器访问?我问,因为可以禁用MySQL服务器的外部连接,更多信息是这里



您的凭据确定(至少其格式)。



否则,您将创建 Class.forName(com.mysql.jdbc.Driver)。newInstance(); 但不要将它分配给 conn 变量。


I am working on a dynamic webapp on eclipse and is trying to access a remote MySQL database. I made sure that all the information is correct. However I can't seem to connect to it. Here's my getConnection method:

public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";
            String user = "username";
            String password = "password";
            conn =  DriverManager.getConnection(url, user, password);
            System.out.println("CONNECTED");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(InstantiationException e){
            e.printStackTrace();
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }
            return conn;

    }

I am trying to see what the error is but because this is a web app, I can't see that system.out.println anywhere, so it's kind of hard for me to debug with this. Any suggestions on how to debug?

Is your MySQL server accessible from out of the server? I am asking because it is possible to disable external connection for MySQL server, more info is here.

Your credentials are OK (at least its format).

Otherwise, you create Class.forName("com.mysql.jdbc.Driver").newInstance(); but you don't assign it to conn variable.