简体   繁体   English

java连接到mysql数据库失败

[英]java connecting to mysql database fails

I am creating a vaadin web application using tomcat and having an external database on another server. 我正在使用tomcat创建vaadin Web应用程序,并在另一台服务器上具有外部数据库。 I am not able to connect to the external database no matter what. 无论如何,我都无法连接到外部数据库。 I have even created a new pure java project using j2se jre 1.6 and trying to connect to the database but the connection still fails. 我什至使用j2se jre 1.6创建了一个新的纯Java项目,并尝试连接到数据库,但连接仍然失败。 I downloaded the mysql driver and copied it into the ~/Library/Java/Extensions and even added/removed from build path and still nothing changed. 我下载了mysql驱动程序并将其复制到〜/ Library / Java / Extensions中,甚至从构建路径中添加/删除了它,但仍然没有任何更改。 I have my firewall off and I dont know what else to do. 我关闭了防火墙,不知道该怎么办。 I believe that my code is correct, but anyway: 我相信我的代码是正确的,但是无论如何:

String host = "jdbc:mysql://db.host.sk:3306/myDB";
    String name = "name";
    String pass = "pass";
    Connection connection;

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("Connecting to database...");
        connection = DriverManager.getConnection(host, name,
                pass);
        System.out.println("SUCCESSFUL!!!");

    } catch (SQLException e) {
        System.out.println("Connecting to database failed");
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Could you please help me with this? 你能帮我这个忙吗? Thank you. 谢谢。

EDIT 编辑

The stack trace: 堆栈跟踪:

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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at     sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at DatabaseHelper.main(DatabaseHelper.java:17)
Caused by: java.net.ConnectException: Operation timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 15 more

检查您的数据库服务已启动并在专用服务器上运行。

Never faced this before however, I read few articles and found the possible reasons could be there's something in between Java and database that is blocking the connection, eg a firewall or server is down/does not support tcp/ip or hostname in jdbc url is not recognized by local dns server, etc etc. Well you can try the following advice(worth giving a shot): 但是,我之前从未遇到过此类文章,我读了几篇文章,发现可能的原因可能是Java与数据库之间存在某种阻止连接的问题,例如防火墙服务器已关闭/不支持jdbc url中的tcp / ip 主机名。无法被本地dns服务器等识别。那么您可以尝试以下建议(值得一试):

  1. Go to Windows services in the control panel and start the MySQL service. 转到控制面板中的Windows服务,然后启动MySQL服务。
  2. Check your wait timeout set on the DB server and update it to, say, 10000 检查您在数据库服务器上设置的等待超时并将其更新为10000

    SET GLOBAL wait_timeout = 10000; SET GLOBAL wait_timeout = 10000;

You can also check a similiar kind of Question having a good answer, posted by Soheil here: Solving a "communications link failure" with JDBC and MySQL 您也可以在Soheil的此处检查类似的问题,该问题具有很好的回答: 在JDBC和MySQL中解决“通信链接失败”

I (un)fortunately ended up using mysql on my machine with eclipse and everything is running smoothly now. 不幸的是,我最终在我的机器上使用了带有eclipse的mysql,现在一切运行顺利。 It's a pity I didnt make it work like I wanted in the first place but the current way suites me better. 遗憾的是我一开始并没有像我想的那样工作,但是当前的方式让我更满意。 I dont have to make any changes and configuration and can finally start coding. 我不必进行任何更改和配置,终于可以开始编码了。 I appreciate all your help. 感谢您的帮助。

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

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