It's not a secret anymore!
The moose likes Object Relational Mapping and the fly likes Cannot connect to MySQL server ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Cannot connect to MySQL server ..." Watch "Cannot connect to MySQL server ..." New topic

Cannot connect to MySQL server ...

irfan romadona

Joined: Aug 10, 2006
Posts: 1

I need some help here. I'm trying to use hibernate with mysql using the tutorial from hibernate source code ( that using hsqldb ). I've change the hibernate.cfg.xml to mysql and when I run it with ant, it shows error likethis :

[java] java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)

There is no way that my Mysql Server is not running because I check it with Mysql Client tools and it connected normally. And I already placed the mysql connector/j jar in /lib directory. I don't know what's wrong with it. Can somebody help me ?

i'm using hibernate 3.1.3, mysql 5.0.22, mysql-connector-java-3.1.13, mysql-connector-java-3.1.13

and this is my hibernate.cfg.xml :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"



<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="">create</property>

<mapping resource="events/Event.hbm.xml"/>


Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 661

1. Try changing your library with appropriate connector file.
2. Check if any other service is running on port 3306. Could even be another MySQL instance.

If you are not laughing at yourself, then you just didn't get the joke.
kamal raja

Joined: May 10, 2009
Posts: 2
I got the same exception & resolved the issue after 3 hectic days.Check if you are using I hibernate3. In this version it is req to explicitly mention the connection class name. Also check if the jar is in classpath. Check steps & comments in below link

autoReconnec=true doesnt seems to work..remove from config file.
I agree. Here's the link:
subject: Cannot connect to MySQL server ...
jQuery in Action, 3rd edition