使用 Java 访问在线数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10438773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing online database with Java
提问by user1170330
I made a Java program which accesses a database. During the development phase I used a local database (XAMPP with MySQL), but when I tried to access 2 different onlinedatabases (a commercial and a free one), I received in both cases the following exception:
我制作了一个访问数据库的Java程序。在开发阶段,我使用了本地数据库(XAMPP 和 MySQL),但是当我尝试访问 2 个不同的在线数据库(商业和免费)时,我在两种情况下都收到以下异常:
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLException: Communications link failure
最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。
I hadn't any problems acessing them with PHP.
我用 PHP 访问它们没有任何问题。
This is my code:
这是我的代码:
private String user = "user1";
private String pass = "pass1";
private String dbClass = "com.mysql.jdbc.Driver";
private String dbDriver = "jdbc:mysql://db4free.net:3306/DBNAME";
private Connection conn = null;
public boolean connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // THIS IS BEING RETURNED
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // THIS IS NOT BEING RETURNED
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
return done;
}
回答by Adrian J. Moreno
A MySQL database not only allows a user to connect using a particular password, but also from an IP range or address.
MySQL 数据库不仅允许用户使用特定密码进行连接,还允许从 IP 范围或地址进行连接。
http://dev.mysql.com/doc/refman/5.1/en/create-user.html
http://dev.mysql.com/doc/refman/5.1/en/create-user.html
CREATE USER 'jeffrey'@'127.0.0.1' IDENTIFIED BY 'mypass';
CREATE USER 'jeffrey'@'127.0.0.1' IDENTIFIED BY 'mypass';
So it's possible that the online databases do not allow that user to connect from your IP address or block.
因此,在线数据库可能不允许该用户从您的 IP 地址进行连接或阻止。
回答by esej
Your code works well for me on a newly created account at db4free:
您的代码在 db4free 新创建的帐户上对我来说效果很好:
Check the following things:
检查以下事项:
Should show either your public ip or a single %
Your username and password is correct, will also verify your account is confirmed.
That the "DBNAME" is correct, log in to "phpMyAdmin-Login:" on db4free.net, the DBNAME will be to the left above "information_schema"
如果“DBNAME”是正确的,登录db4free.net上的“phpMyAdmin-Login:” ,DBNAME 将在“information_schema”上方的左侧
And finally that your local firewall doesn't block you:
最后,您的本地防火墙不会阻止您:
telnet db4free.net 3306 (from a command line/prompt)
(if you get a prompt, your firewall lets you through.)
(如果您收到提示,您的防火墙会让您通过。)
回答by Ellipsis
There is no port number mentioned in your code. Here is the example of the working code.
您的代码中没有提到端口号。这是工作代码的示例。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(" Unable to load driver. ");
}
String url = "jdbc:mysql://DBSERVERNAME:3306/DATABASENAME";
String username = "user";
String password = "pass";
try {
this.conn = DriverManager.getConnection(url, username, password);
System.out.println(" Connection Established. ");
} catch (SQLException e) {
System.out.println(" Error connecting to database: "
+ e);
}
However, There can be several reasons for this problem.
但是,此问题可能有多种原因。
- Check in your MySQL server settings if the hostmachine (machine you are running this code from) has permission to access the MySql server?
- Verify the JDBC URL, check port#, ip address/hostname
- Check if there is anything (like firewall, proxy) blocking connections from host to db server
- Try pinging the DB server from the hostmachine
- 如果主机(运行此代码的机器)有权访问 MySql 服务器,请检查您的 MySQL 服务器设置?
- 验证 JDBC URL,检查端口号、IP 地址/主机名
- 检查是否有任何东西(如防火墙、代理)阻止了从主机到数据库服务器的连接
- 尝试从主机 ping 数据库服务器
Similar post: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure
类似帖子:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure
回答by Kiran Kumar
I think it might be because of firewall issue, usually the server might be listening on the 127.0.0.1 ip address, so you access my sql only from the local system, change it to 0.0.0.0 in the configuration file, then it will start working.
我想可能是防火墙的问题,通常是服务器监听了127.0.0.1的ip地址,所以你只能从本地系统访问我的sql,在配置文件里改成0.0.0.0,然后就启动了在职的。
回答by ajay.r.kakkar
static private String user = "ajaykakkar";
static private String pass = "ajaykakkar7432";
static private String dbClass = "com.mysql.jdbc.Driver";
// its - - - "jdbc:mysql://www.db4free.net:3306/yourdatabasename_ark";
// 它的 - - - "jdbc:mysql://www.db4free.net:3306/yourdatabasename_ark";
static private String dbDriver = "jdbc:mysql://www.db4free.net:3306/yourdatabasename_ark";
static private Connection conn = null;
public static boolean connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // THIS IS BEING RETURNED
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // THIS IS NOT BEING RETURNED
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
return done;
}
public static void main(String args[]) {
connect();
}
// done by @ ajay.r.kakkar
// 由@ajay.r.kakkar 完成