当前位置: 动力学知识库 > 问答 > 编程问答 >

java connecting to mysql database fails

问题描述:

I am creating a vaadin web application using tomcat and having an external database on another server. 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. I downloaded the mysql driver and copied it into the ~/Library/Java/Extensions and even added/removed from build path and still nothing changed.

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

网友答案:

Check your database Service is up and running on dedicated server.

网友答案:

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, e.g. 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):

  1. Go to Windows services in the control panel and start the MySQL service.
  2. Check your wait timeout set on the DB server and update it to, say, 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

网友答案:

I (un)fortunately ended up using mysql on my machine with eclipse and everything is running smoothly now. 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: