java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure -
i'm working on getting database talk java programs.
can give me quick , dirty sample program using jdbc?
i'm getting rather stupendous error:
exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.communicationsexception: communications link failure last packet sent server 0 milliseconds ago. driver has not received packets server. @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:39) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:27) @ java.lang.reflect.constructor.newinstance(constructor.java:513) @ com.mysql.jdbc.util.handlenewinstance(util.java:409) @ com.mysql.jdbc.sqlerror.createcommunicationsexception(sqlerror.java:1122) @ com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2260) @ com.mysql.jdbc.connectionimpl.<init>(connectionimpl.java:787) @ com.mysql.jdbc.jdbc4connection.<init>(jdbc4connection.java:49) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:39) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:27) @ java.lang.reflect.constructor.newinstance(constructor.java:513) @ com.mysql.jdbc.util.handlenewinstance(util.java:409) @ com.mysql.jdbc.connectionimpl.getinstance(connectionimpl.java:357) @ com.mysql.jdbc.nonregisteringdriver.connect(nonregisteringdriver.java:285) @ java.sql.drivermanager.getconnection(drivermanager.java:582) @ java.sql.drivermanager.getconnection(drivermanager.java:207) @ sqltest.main(sqltest.java:22) caused by: com.mysql.jdbc.exceptions.jdbc4.communicationsexception: communications link failure last packet sent server 0 milliseconds ago. driver has not received packets server. @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:39) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:27) @ java.lang.reflect.constructor.newinstance(constructor.java:513) @ com.mysql.jdbc.util.handlenewinstance(util.java:409) @ com.mysql.jdbc.sqlerror.createcommunicationsexception(sqlerror.java:1122) @ com.mysql.jdbc.mysqlio.<init>(mysqlio.java:344) @ com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2181) ... 12 more caused by: java.net.connectexception: connection refused @ java.net.plainsocketimpl.socketconnect(native method) @ java.net.plainsocketimpl.doconnect(plainsocketimpl.java:333) @ java.net.plainsocketimpl.connecttoaddress(plainsocketimpl.java:195) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:182) @ java.net.sockssocketimpl.connect(sockssocketimpl.java:432) @ java.net.socket.connect(socket.java:529) @ java.net.socket.connect(socket.java:478) @ java.net.socket.<init>(socket.java:375) @ java.net.socket.<init>(socket.java:218) @ com.mysql.jdbc.standardsocketfactory.connect(standardsocketfactory.java:256) @ com.mysql.jdbc.mysqlio.<init>(mysqlio.java:293) ... 13 more
contents of test file:
import com.mysql.jdbc.*; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class sqltest { public static void main(string [] args) throws exception { // class.forname( "com.mysql.jdbc.driver" ); // in init // // edit jdbc url connection conn = drivermanager.getconnection( "jdbc:mysql://localhost:3306/projects?user=user1&password=123"); // statement st = conn.createstatement(); // resultset rs = st.executequery( "select * table" ); system.out.println("connected?"); } }
so, have
com.mysql.jdbc.exceptions.jdbc4.communicationsexception: communications link failure
java.net.connectexception: connection refused
i'm quoting this answer contains step-by-step mysql+jdbc tutorial:
if
sqlexception: connection refused
orconnection timed out
or mysql specificcommunicationsexception: communications link failure
, means db isn't reachable @ all. can have 1 or more of following causes:
- ip address or hostname in jdbc url wrong.
- hostname in jdbc url not recognized local dns server.
- port number missing or wrong in jdbc url.
- db server down.
- db server doesn't accept tcp/ip connections.
- db server has run out of connections.
- something in between java , db blocking connections, e.g. firewall or proxy.
to solve 1 or other, follow following advices:
- verify , test them
ping
.- refresh dns or use ip address in jdbc url instead.
- verify based on
my.cnf
of mysql db.- start db.
- verify if mysqld started without
--skip-networking option
.- restart db , fix code accordingly closes connections in
finally
.- disable firewall and/or configure firewall/proxy allow/forward port.
Comments
Post a Comment