connecting to remote MySQL server using JDBC issue

2019-08-20 05:08发布

问题:

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?

回答1:

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.



回答2:

In web applications, System.out prints to the server's logs file. If you are using tomcat, see logs directory inside tomcat's base directory.



回答3:

If you are still struggling to see the System out in logs, try to test the class from standalone java class, you will clearly see what the problem is.