This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I have problem with connecting to MySQL Server installed on Ubuntu 16.04 server but I got these Errors:
Exception in thread "main" 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:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at schoolcontroller.tools.DataBase.mysql.DataAccess.connect(DataAccess.java:45)
at schoolcontroller.tools.DataBase.mysql.DataAccess.main(DataAccess.java:40)
I can connect to the server using MySQL Client applications like Navicate and MySQL workbench with no error.
Here is my code
public DataAccess() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
System.out.println("Connecting");
new DataAccess().connect();
}
public void connect() throws SQLException {
String connection = "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8";
conn = DriverManager.getConnection(String.format(connection, "IPAddress", "3306", "DBname"), "user", "pass");
System.out.println("Connected");
st = conn.createStatement();
}
The port and the username the database name and and the IP is correct.
You could try to register the driver class to the DriverManager
String driver = "com.mysql.jdbc.Driver"
Class driverClass = Class.forName(driver);
Driver driverObj = (Driver)driverClass.newInstance();
DriverManager.registerDriver(driverObj);
If you still have problems, then you could try to deregister all drivers in first place and then add the driver again like this:
String driver = "com.mysql.jdbc.Driver"
List<Driver> allDriver = new ArrayList<Driver>();
Enumeration drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements())
allDriver.add((Driver)drivers.nextElement());
for (Driver d : allDriver)
DriverManager.deregisterDriver(d);
Class driverClass = Class.forName(driver);
Driver driverObj = (Driver)driverClass.newInstance();
DriverManager.registerDriver(driverObj);
Related
I am getting the following exception :-
java.sql.SQLException: [DataDirect][OpenEdge JDBC Driver][OpenEdge] Access denied(Authorisation failed)
at com.ddtek.jdbc.openedge.client.ddd.aw(Unknown Source)
at com.ddtek.jdbc.openedge.client.ddd.j(Unknown Source)
at com.ddtek.jdbc.openedge.OpenEdgeImplConnection.j(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.b(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.k(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.b(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.a(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.ncr.sj250216.JdbcTest.main(JdbcTest.java:18)
There is no compile time error in my code, i am using the right jars as referenced libraries.
package com.ncr.sj250216;
import java.sql.*;
public class JdbcTest
{
public static String
URL="jdbc:datadirect:openedge://sun3899.daytonoh.ncr.com:28409;DatabaseName=dispatch";
public static String username="CSMSRUP";
public static String password=null;
public static void main(String [] args)
{
try
{
Class.forName ("com.ddtek.jdbc.openedge.OpenEdgeDriver");
System.out.println("Driver loaded");
Connection conn = DriverManager.getConnection(URL,username,password);
System.out.println("Connected");
Statement stmt = conn.createStatement();
System.out.println("Statement created");
ResultSet rs = stmt.executeQuery("select remark from pub.dupdate_remarks");
System.out.println("ResultSet : \n");
while (rs.next())
{
System.out.println(rs.getString(1));
}
System.out.println("AFTER CRUISING THROUGH THE RESULT SET");
rs.close();
stmt.close();
conn.close();
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
Access denied and Authorization failed mean that you either have the wrong userid & password or you are attempting to connect to the wrong db.
If you have access to the server that the db runs on you can test your credentials using Progress' sqlexp command line tool:
sqlexp -user userName -password passWord -db dnName -S servicePort
Or you could use any popular Windows based SQL querying tool to try the connection...
I am attempting to connect to an Oracle database through Java with the Oracle JDBC driver with the following code (obscuring the host, service, user, and password):
import java.sql.*;
public class Main {
public Main () {
try {
String host = "HOST_NAME";
String port = "1521";
String service = "SERVICE_NAME";
String user = "SCHEMA_USER";
String password = "SCHEMA_PASSWORD";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);
connection.close ();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String args) {
new Main ();
}
}
However, I receive the following error:
java.sql.SQLException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.acxiom.axle.reporting.database.DatabaseConnection.connect(DatabaseConnection.java:23)
at com.acxiom.axle.reporting.Reporting.establishDatabaseConnection(Reporting.java:53)
at com.acxiom.axle.reporting.Reporting.beginReporting(Reporting.java:20)
at com.acxiom.axle.reporting.Entry.<init>(Entry.java:28)
at com.acxiom.axle.reporting.Entry.main(Entry.java:118)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
... 11 more
Caused by: java.net.UnknownHostException: null
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)
... 16 more
The strange thing is, I can connect to the database from PL/SQL Developer, I can ping the remote host, and I can telnet to the remote host on port 1521.
Why would only Java appear to give an UnknownHostException, but I can connect and ping the host with other applications?
EDIT: I removed "hr/hr" from the connection string above. I have tried the connection as-is with it removed and still receive the same error. I've also tried changing the connection string to match the version morgano listed in his answer, with the same result. Finally, I tried to change the port number to a port I know it's not listening on, and it still receives the same error.
The connection string is of the correct format. The problem is that the host isn't set. It's null, or perhaps the string "null". There's simply no other way to generate the error message java.net.UnknownHostException: null
In your sample code, you write String host = "HOST_NAME";. This makes it look like in your real code you are assigning a constant string to the variable host. However, I'm going to stick my neck out and say that in your real code you are not doing this. You are looking up the host name from somewhere, this lookup fails for some reason, you don't check this, and hence you pass a null value into the connection string.
Your JDBC url is wrong, according to the documentation it should be something like:
jdbc:oracle:driver_type:[username/password]#//host_name:port_number/service_name
In your case your code would be something like:
import java.sql.*;
public class Main {
public Main () {
try {
String host = "HOST_NAME";
String port = "1521";
String service = "SERVICE_NAME";
String user = "SCHEMA_USER";
String password = "SCHEMA_PASSWORD";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:#//" + host
+ ":" + port + "/" + service, user, password);
connection.close ();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String args) {
new Main ();
}
}
I dont know much about the connection proccess i'm trying to connect wordpress database without creating new database with my remotedesktop IP in mysql. Normally i can connect localhost and i can connect to database that created with remote ip. In my server mydatabase host name/Ip adress is localhost, as i said i dont know the whole connection between url request,wp-config file and mysql. I think i'm sending request with url to wp-config and it is connecting to my localhost. Anyway i want to connect to my remotedesktop's localhost with JAVA but i couldn't. Let me explain with code . Hope i could explain myself.
Thanks in advance.
Here are the codes :
wp-config.php
define('DB_NAME', 'mydbname');
define('DB_USER', 'mydbuser');
define('DB_PASSWORD', 'mydbpass');
define('DB_HOST', '127.0.0.1');
My connection code :
public class Dt_page {
public static DataSource getDataSource(int server) {
String database_driver = null;
String database_adres = null;
String dbuser = null;
String dbpass = null;
ConnectionFactory connectionFactory;
switch (server) {
case 1://profile server
database_driver = "com.mysql.jdbc.Driver";
database_adres = "jdbc:mysql://MY_REMOTE_DESKTOP_IP:3306/?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true";
dbuser = "mydbuser";
dbpass = "mydbpass";
}
try {
Class.forName(database_driver);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Dt_page.class.getName()).log(Level.SEVERE, null, ex);
}
Properties props = new Properties();
props.put("user", dbuser);
props.put("password", dbpass);
props.put("autoReconnect", "true");
props.put("autoReconnectForPools", "true");
props.put("validationQuery", "SELECT 1 from dual;");
props.put("testWhileIdle","true");
connectionFactory = new DriverManagerConnectionFactory(database_adres, props);
GenericObjectPool connectionPool = new GenericObjectPool(null, 12, GenericObjectPool.WHEN_EXHAUSTED_GROW, 60*30, 12,true,true);
GenericKeyedObjectPoolFactory keyedObjectPoolFactory = new GenericKeyedObjectPoolFactory(null);
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory,
connectionPool,
keyedObjectPoolFactory,
"select 1",
30,
null,
false,
true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
And SQL
QueryRunner qr;
qr = new QueryRunner(Dt_page.getDataSource(1));
try {
List<Object[]> v = qr.query("Select meta_value from mydbname.wp_favorite", new ArrayListHandler());
System.out.println(v.size());
for (Object[] objects : v) {
for (Object object : objects) {
System.out.println(object);
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
Error Log:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2367)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2288)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
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:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:187)
at org.apache.commons.dbcp.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:78)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1188)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
at org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:175)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:304)
at tazedizi.TazeDizi.main(TazeDizi.java:29)
Caused by: 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:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2306)
... 19 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
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)
... 21 more
Two things to check:
Are you sure that your mysql server is configurated to listend on MY_REMOTE_DESKTOP_IP ?
Are you sure that your firewall (iptable...) allows connexion on MY_REMOTE_DESKTOP_IP on tpc port 3306 ?
I am trying to run a JAVA program which inserts values into mySQL database. When I run the program, it says Connection refused: connect I have included mySQL jars also. What is the problem? Can anyone help me out
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DataLogs {
public static void main(String[] args) throws SQLException,ClassNotFoundException {
Connection connection = null;
try {
//int i=0;
String strDate="", strTime="";
// Register MySQL JDBC driver to be known by
// the DriverManager object.
Class.forName("com.mysql.jdbc.Driver");
// Get a connection to database. We prepare the
// connection information here such as database
// url, user and password.
String url = "jdbc:mysql://localhost:8080/sampledatabase";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
// Create a statement object instance from the
// connection
Statement stmt = connection.createStatement();
// We are going to execute an insert statement.
// First you have to create a table that has an
// ID, NAME and ADDRESS field. For ID you can use
// an auto number, while NAME and ADDRESS are
// VARCHAR fields.
for(int i=1;i<=100;i++){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String formattedDate = sdf.format(date);
strDate=getFormatedDate();
strTime=getTime();
String mm=strDate.substring(3, 5);
String yy=strDate.substring(8, 10);
String hh=strTime.substring(0, 2);
String mi=strTime.substring(3, 5);
String ss=strTime.substring(6, 8);
String dd=strDate.substring(0, 2);
String date1 = ""+yy+"-"+mm+"-"+dd+" "+hh+":"+mi+":"+ss;
String sql= "INSERT INTO `site_values_"+i+"` VALUES("+i+",5316,0,0,130,89,904,171,1006,96,4000,"+ss+","+mi+","+hh+","+dd+","+mm+","+yy+",84753,0,0,0,0,0,0,0,0,0,'Short Circuit Shutdown','Shutdown',1,'"+date1+"')";
// Call an execute method in the statement object
// and passed the sql or query string to it.
stmt.execute(sql);
}
// After this statement is executed you'll have a
// record in your users table.
} catch (ClassNotFoundException e) {
System.err.println("Could not load database driver!"+e);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public static String getFormatedDate() {
String strDate="";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfdate = new SimpleDateFormat("dd/MM/yyyy");
strDate= sdfdate.format(cal.getTime());
return strDate;
}//getFormatedDate
public static String getTime() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
java.util.Date date = new java.util.Date();
String datetime = dateFormat.format(date);
return datetime;
} //getTime
}
This is what I see in the console
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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at solarnmsstandard.DataLogs.main(DataLogs.java:27)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 15 more
Check if your connection parameters are correct (username, password) and especially the port where the MySql server is running.
check whether your mysql server is running or not, if it is not running then you will get this exception
java.net.ConnectException: Connection refused: connect
to run server
go to <mysql installation path>/bin/ and run mysqld.. to start the mysql server.
even after if you get the same error, provide the full stack trace.
check your port 8080 is free or not
if you are use linux run the follwing command and restart your server.
sudo kill sudo lsof -t -i:8080
sudo kill sudo lsof -t -i:8005
sudo kill sudo lsof -t -i:8009
I would be very suspicious of port 8080 being used as the port for MySQL; it really should be port 3306. So that would have to be checked properly as 8080 is usually the port number for an Application Server instance (such as GlassFish or Tomcat).
I am working on a GWT project in which I need to make some MySQL queries. I have handled RPC properly and in the server-side I am trying to make a mysql connection but am running into an exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
.
.
.
more
.
.
.
I created a very simple java class to test out my code to confirm that my connection syntax and use is proper, and have confirmed that syntax is not a problem because with the very same code in a simple only main method java app I can create a connection and query the database properly. I have made sure to have mysql-connector-java-5.1.16-bin.jar in the classpath as well as the /lib folder in the WEB-INF folder.
Here is the class that I am using in order to create a connection:
public class DB_Connection {
protected Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///",
"",
"");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return conn;
}
}
public class Login extends DB_Connection {
public User getUser(String email) {
User user = new User();
user.setUserEmail(email);
String query = "";
try {
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
user.setUserId(result.getInt("rvuser_id"));
System.out.println(user.getUserId());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return user;
}
It seems my problem happens all the way at the beginning in DB_Connection. Does anyone know what is making this happen? It is strange to me, because as a standalone the code works fine.
Another thing I just realized that I am at home now and don't even have access to that database server as it is on a local network and I am not connected through VPN. So it must be failing somehow before it even attempts to make the connection.
Thanks!
com.google.appengine ..
Does it means you are deploying on Google App Engine?
If so, MySQL,JDBC and many other things are restricted.