Java connection failed with MySQL -
this question has answer here:
- connect java mysql database 12 answers
i have created mysql database @ hostinger.co.uk
, trying make connection between database , java application, , have written java code:
import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; public class db_connection { public static void main(string arg[]) throws exception { makeconnection("u335300563_myname", "pass12345"); } public static void makeconnection(string username, string password) throws exception { class.forname("com.mysql.jdbc.driver"); connection connection = drivermanager.getconnection("jdbc:mysql://u335300563_database",username,password); preparedstatement statement = connection.preparestatement("select * example"); resultset result = statement.executequery(); for(int i=1; result.next(); i++) system.out.println(result.getstring(i)); } }
but throwing exception is:
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(unknown source) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) @ java.lang.reflect.constructor.newinstance(unknown source) @ com.mysql.jdbc.util.handlenewinstance(util.java:377) @ com.mysql.jdbc.sqlerror.createcommunicationsexception(sqlerror.java:1036) @ com.mysql.jdbc.mysqlio.<init>(mysqlio.java:338) @ com.mysql.jdbc.connectionimpl.coreconnect(connectionimpl.java:2232) @ com.mysql.jdbc.connectionimpl.connectonetryonly(connectionimpl.java:2265) @ com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2064) @ com.mysql.jdbc.connectionimpl.<init>(connectionimpl.java:790) @ com.mysql.jdbc.jdbc4connection.<init>(jdbc4connection.java:44) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(unknown source) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) @ java.lang.reflect.constructor.newinstance(unknown source) @ com.mysql.jdbc.util.handlenewinstance(util.java:377) @ com.mysql.jdbc.connectionimpl.getinstance(connectionimpl.java:395) @ com.mysql.jdbc.nonregisteringdriver.connect(nonregisteringdriver.java:325) @ java.sql.drivermanager.getconnection(unknown source) @ java.sql.drivermanager.getconnection(unknown source) @ db_connection.makeconnection(db_connection.java:14) @ db_connection.main(db_connection.java:9) caused by: java.net.unknownhostexception: u335300563_database @ java.net.inet6addressimpl.lookupallhostaddr(native method) @ java.net.inetaddress$1.lookupallhostaddr(unknown source) @ java.net.inetaddress.getaddressesfromnameservice(unknown source) @ java.net.inetaddress.getallbyname0(unknown source) @ java.net.inetaddress.getallbyname(unknown source) @ java.net.inetaddress.getallbyname(unknown source) @ com.mysql.jdbc.standardsocketfactory.connect(standardsocketfactory.java:190) @ com.mysql.jdbc.mysqlio.<init>(mysqlio.java:297) ... 16 more
so please tell problem?
caused by: java.net.unknownhostexception: u335300563_database
program failed identify host of mysql db.
in program, have pass correct hostname or ip address of system hosted mysql:
following line need correct
drivermanager.getconnection("jdbc:mysql://u335300563_database" ,username,password);
to
drivermanager.getconnection("jdbc:mysql://<ip address of mysql db server or correct hostname port number >", username,password);
Comments
Post a Comment