使用JDBC问题连接到远程MySQL服务器 [英] connecting to remote MySQL server using JDBC issue

查看:149
本文介绍了使用JDBC问题连接到远程MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在eclipse上处理一个动态的webapp,并试图访问一个远程的MySQL数据库。我确保所有的信息是正确的。但是我似乎无法连接到它。这是我的getConnection方法:

  public static Connection getConnection()throws SQLException {
Connection conn = null;
尝试{
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;

}

我试图看看错误是什么,是一个网络应用程序,我看不到system.out.println在任何地方,所以我很难调试这个。任何关于如何调试的建议?

解决方案

您的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.

这篇关于使用JDBC问题连接到远程MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆