使用JDBC问题连接到远程MySQL服务器 [英] connecting to remote MySQL server using JDBC issue
问题描述
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屋!