Problem with MySQL Connector/J

I'm using NetBeans IDE 5.5 with JDK 1.6.0 on Fedora Core 6.

When I ran "./java -version

" from the JDK_HOME, I got this message:

java version"1.6.0"

Java(TM) SE Runtime Environment (build 1.6.0-b105)

Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

Further the "mysql --version

" command had the following output :

mysql Ver 14.12 Distrib 5.0.27,for redhat-linux-gnu (i686) using

readline 5.0

I downloaded the JDBC Connector (version 5.0) for MySQL : MySQL

Connector/J, and installed it using the instruction outlined in the

Connector/J documentation (by setting the classpath in the

$USER_HOME/.bash_profile).

I attached the jar file of the Connector/J

(mysql-connector-java-5.0.4-bin.jar) in the NetBeans project as well.

From within the NetBeans IDE, when I tried to run my Java application

which tried to connect to the mysql database, the application terminated

with the following output:

com.mysql.jdbc.CommunicationsException: Communications link failure due

to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException

MESSAGE: java.net.ConnectException: Connection refused

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused

at

com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)

at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)

at com.mysql.jdbc.Connection.<init>(Connection.java:1531)

at

com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)

at java.sql.DriverManager.getConnection(DriverManager.java:582)

at java.sql.DriverManager.getConnection(DriverManager.java:185)

at test.DB.ConnectDB(DB.java:37)

at test.DB.<init>(DB.java:29)

at test.Main.main(Main.java:30)

** END NESTED EXCEPTION **

But the same code runs without any problem with the Eclipse 3.2 IDE on

the same system. I added a new Java Platform to the Netbeans IDE (GNU

libgcj 4.1.1 20061011 (Red Hat 4.1.1-30)). The same program ran

successfully with the new platform set in the project property. So I

concluded that may be the problem is with the SUN JDK 1.6.0. Is there

any other way to register the driver with JDK 1.6.0 (except the method

outlined in the Connector/J documentation, i.e. setting the classpath to

the driver's jar file)?

Moreover when trying to add a persistent unit for a J2EE application,

the same error occurs while connecting to my database using the MySQL

Connector/J Driver for JDBC. I've stuck to this problem since more than

a month and I hope if some one in the forum could help me.

Thanks in advance.

Regards

Aks

[3037 byte] By [askme_aksa] at [2007-11-26 16:35:57]
# 1
Can you check if you have the port number for the connection correct?
g_magossa at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> Connection refused

means: JDBC has connected the MySQL Server, but the MySQL Server replied 'You are not allowed to access anything here'.

Possible Reasons:

- User / Password is unknown or wrong

double check that

- User is not allowed to connect with the actual IP

Query as MySQL user 'root' the user table and check the 'Host' column

Sample:

SELECT Host, User FROM mysql.`user`

Host, User

'localhost', 'root'

'%', 'TomJones'

-> root is only allowed to connect from IP 'localhost'

and TomJones may connect from any IP

hope it helps you

Franco

franco_weichela at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Dear Franco,

Thankyou for your suggestion. I rechecked the user name and password, and both of them are right. And when I compiled the same project (as I had already stated) with GNU libGCJ, the same project ran successfully without any error, but as you know that the NetBeans IDE uses the Sun JDK, I'm getting this problem.

I queried the "user" for the host settings as you had mentioned:

SELECT Host,User FROM mysql.user;

and I came to know that the user "root" was configured to login through the hosts "localhost" and "localhost.localdomain", and as my system was named "aks.localdomain", I added a host "%" to the user table for the user root, so that the user root may connect from any host to the MySQL server.

But even though, I could not connect to the server through NetBeans IDE. The same problem occured again. Hope you could help me.

Thanks.

Regards

Aks

askme_aksa at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

Hi,

> ...user "root" was configured to login through the hosts "localhost"

> and "localhost.localdomain", and as my system was

> named "aks.localdomain"

Not the names are importent, but it IP that are resolved by the names.

What is the IP of the MySQL Server and what ist the IP of aks.localdomain

i am not sure, but i think you can not add '%' to host, you have to replace the old values.

update mysql.user set host = '%' where user = 'root'

Or even better... don't work as root. Create a new user with the privileges he need and use this user to access the database. You should not work as Admin, not in Windows/Unix or Linux and also not in a database.

Franco

franco_weichela at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Hi,i also found a this: http://forums.mysql.com/read.php?39,24742,24742#msg-24742there could be an other problem.Maybe the change in the my.cnf as suggested there will help you.Franco
franco_weichela at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Dear Franco, Thanks for your suggestion and the resources. I could not conect to localhost, but instead I could connect to the same server using the IP address. What so ever I'm able to continue my project with it.Thanx a lot.RegardsAks
askme_aksa at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
I had the same problem when connecting to jdbc:mysql://localhost:3306/dbname. The solution for my case is adding the following entry to the /etc/hosts file.127.0.0.1localhost
Jacky.LUa at 2007-7-8 23:00:50 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...