简体   繁体   English

使用JDBC连接到mysql远程数据库?

[英]Connect to mysql remote database using JDBC?

I'm a java beginner and I'm working on a simple application which connects to a remote mysql database using JDBC. 我是一个java初学者,我正在研究一个使用JDBC连接到远程mysql数据库的简单应用程序。 I've tested it locally and it works just fine, however I cannot get it to work on for my remote server. 我已经在本地进行了测试,它运行得很好,但我不能让它为我的远程服务器工作。 I don't think its of much use but heres the code: 我不认为它有多大用处,但是代码是:

Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";

    try {
        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(dburl, userName, passWord);
        Statement st = connection.createStatement();                                 

        String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
        int rsI = st.executeUpdate(query);
        System.out.println("Hi");
        }catch (Exception e) {
        System.out.println(e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
                System.out.println("Database connection terminated");
            } catch (Exception e) { /* ignore close errors */ }
        }
    }

When I run this, I get the following message: 当我运行它时,我收到以下消息:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.

I'm pretty sure it must be some kind of server configuration issue. 我很确定它必须是某种服务器配置问题。

 Notes:
 Username, password, IP, database name, etc. are just examples.
  1. This could be a firewall problem, or a configuration problem. 这可能是防火墙问题或配置问题。 But I don't think it is a coding problem at all - you need to start troubleshooting the connection. 但我认为这根本不是编码问题 - 您需要开始对连接进行故障排除。

  2. Trouble shoot by attempting to use third party client apps to connect to mysql. 通过尝试使用第三方客户端应用程序连接到mysql进行故障排除。 This will indicate whether it is configured for external access. 这将指示是否配置为外部访问。 Although it doesn't ensure that JDBC is visible from the outside, it does rule out some potential firewall problems. 虽然它不能确保从外部看到JDBC,但它确实排除了一些潜在的防火墙问题。

  3. Follow this guide to help you mess with your configurations 请按照本指南帮助您搞乱配置

Remote MYSQL Database Access 远程MYSQL数据库访问

If you are still stuck, it could be a coding problem so check out this page: 如果您仍然卡住,可能是编码问题,请查看此页面:

How to connent to a remote mysql database with java? 如何用java连接远程mysql数据库?

PS I am assuming you are using unix as the operating system. PS我假设你使用unix作为操作系统。

我猜314159265可以被某个地址替换....比如jdbc:mysql:// localhost:3306 /或jdbc:mysql://127.0.0.1:3306 /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM