Just to be complete, let me give an overview of my problems and the Java code.
A few months ago, I finished my First Java Server Faces web application. This web application uses a MySQL database. I have a local version and a version installed on a web server. This worked fine.
Now I want to start version 2 of this JSF web app. I started my version 1 and all of a sudden, the connection with the database does not function any more. I googled around and it seems I am not the only one.
On the web page
http://www.experts-exchange.com/Programming/Languages/Java/Q_24219843.html
I read the following : “
Hi all, i'm getting an unusual problem in connecting to mySQL from java. My system worked fine for months but now all of a sudden i'm getting a problem. I've tried it inside and outside of my eclipse environment and i've reinstalled the mySQL database too. I can only assume that perhaps something in my OS environment has changed but i cant think what...
I have an abstract class GeneralDao which contains the method to connect to the database.
package org.gco.dao;
import java.util.ResourceBundle;
import java.sql.*;
import org.gco.logging.*;
public abstract class GeneralDao {
protected Connection getConnection()
throws java.sql.SQLException
{
Connection dbconn = null;
Log4jBoek.meld("GeneralDao - start van getConnection",0);
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Log4jBoek.meld("GeneralDao - klasse geladen van JDBC driver",0);
dbconn = DriverManager.getConnection("jdbc:mysql://localhost/gco","gert","wachtwoord");
Log4jBoek.meld("GeneralDao - connectie met databank gelegd",0);
}
catch (SQLException e)
{
Log4jBoek.meld("GeneralDao fout bij getConnection - SQLException ",2);
e.printStackTrace();
}
catch (InstantiationException e)
{
Log4jBoek.meld("GeneralDao fout bij getConnection - InstantiationException ",2);
e.printStackTrace();
} catch (IllegalAccessException e)
{
Log4jBoek.meld("GeneralDao fout bij getConnection - Illegal Access ",2);
e.printStackTrace();
} catch (ClassNotFoundException e)
{
Log4jBoek.meld("GeneralDao fout bij getConnection - driver klasse onbekend ",2);
e.printStackTrace();
}
Log4jBoek.meld("GeneralDao connectie is gemaakt ",0);
return dbconn;
}
} // end GeneralDao
I have a class GebruikerDao which extends the GeneralDao.
package org.gco.engelbert.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.gco.dao.GeneralDao;
public class GebruikerDao extends GeneralDao {
public boolean zoekGebruiker(String _naam, String _wachtwoord)
throws SQLException
{
StringBuffer zoekString = new StringBuffer();
zoekString.append ("select * from gebruiker ");
zoekString.append("where naam=? and wachtwoord=? ");
Connection con = getConnection();
if (con == null)
{
System.out.println("connectie is null !");
// Log4JBoek.meld("connectie met databank is null", 2);
}
PreparedStatement ps = con.prepareStatement(zoekString.toString());
ps.setString(1,_naam);
ps.setString(2,_wachtwoord);
ResultSet rs = ps.executeQuery();
if (rs.next() )
{
rs.close();
ps.close();
con.close();
return true;
}
else {
ps.close();
con.close();
return false;
}
} // end zoekGebruiker
} // end GebruikerDao
To test the connection, I’ve written a small class with main() method.
package org.gco.test;
import org.gco.engelbert.dao.*;
import java.sql.SQLException;
public class KoppelingDatabankTest
{
/**
* @param args
*/
public static void main(String[] args)
{
String naam, wachtwoord;
naam = "Loes";
wachtwoord = "wachtwoord";
boolean gevonden = false;
GebruikerDao gebruikerDao = new GebruikerDao();
try {
gevonden = gebruikerDao.zoekGebruiker(naam, wachtwoord);
}
catch (SQLException SQLex)
{
System.out.println("SQLException in KoppelingDatabankTest");
}
System.out.printf("de waarde van gevonden met %s en %s is %s\n", naam, wachtwoord, gevonden);
} /* main() */
} /* class KoppelingDatabankTest */
When I execute this class with main() method, the output is the following.
klasse van driver geladen
GeneralDao - Fout bij getConnection - SQLException
com.mysql.jdbc.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.gco.dao.GeneralDao.getConnection(GeneralDao.java:74)
at org.gco.engelbert.dao.GebruikerDao.zoekGebruiker(GebruikerDao.java:33)
at org.gco.test.KoppelingDatabankTest.main(KoppelingDatabankTest.java:21)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
... 8 more
-----------------
------------->>>
Communications link failure
Last packet sent to the server was 0 ms ago.
08S01
Has anyone got the same experience with a connection to a MySQL database working fine until recently ? Any ideas on what the cause is or suggestions for the solution ?