You are not logged in.

#1 2010-03-05 14:31:34

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

[SOLVED] Mysql + Java (JDBC driver problem)

Hello!
I want to connect to mysql server with Java, but i got an error like this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I found in Wiki some instruction, i installed mysql-jdbc from AUR but does not work :S

Can someone help me to get this work ?

PS. i try with the program from the wiki

import java.sql.*;
import java.util.Properties;
public class DBDemo
{
 // The JDBC Connector Class.
 private static final String dbClassName = "com.mysql.jdbc.Driver";
 // Connection string. emotherearth is the database the program
 // is connecting to. You can include user and password after this
 // by adding (say) ?user=paulr&password=paulr. Not recommended!
 private static final String CONNECTION =
                         "jdbc:mysql://127.0.0.1/emotherearth";
 public static void main(String[] args) throws
                            ClassNotFoundException,SQLException
 {
   System.out.println(dbClassName);
   // Class.forName(xxx) loads the jdbc classes and
   // creates a drivermanager class factory
   Class.forName(dbClassName);
   // Properties for user and password. Here the user and password are both 'paulr'
   Properties p = new Properties();
   p.put("user","paulr");
   p.put("password","paulr");
   // Now try to connect
   Connection c = DriverManager.getConnection(CONNECTION,p);
   System.out.println("It works !");
   c.close();
   }
}

Last edited by hBd (2010-03-06 19:29:48)

Offline

#2 2010-03-05 15:49:33

SiC
Member
From: Liverpool, England
Registered: 2008-01-10
Posts: 429

Re: [SOLVED] Mysql + Java (JDBC driver problem)

Your JDBC .jar file is not in the class path.  Try setting the classpath to the folder where the file is and run the Java app again

so

CLASSPATH=/path/to/jdbc/driver:$CLASSPATH java -jar myapp.jar

would work

Offline

#3 2010-03-05 16:18:33

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: [SOLVED] Mysql + Java (JDBC driver problem)

Thanks for the help, i make it different but u show me the way tongue
I make a link ln -s /usr/share/java/mysql-jdbc/mysql-connector-java-bin.jar $JAVA_HOME/jre/lib/ext/
and it works but now i get another error msg :S

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:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1119)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2257)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:784)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:354)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at DBDemo.main(DBDemo.java:24)
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:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1119)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2178)
    ... 12 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:525)
    at java.net.Socket.connect(Socket.java:475)
    at java.net.Socket.<init>(Socket.java:372)
    at java.net.Socket.<init>(Socket.java:215)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
    ... 13 more

hmm hmm

Offline

#4 2010-03-05 21:33:26

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: [SOLVED] Mysql + Java (JDBC driver problem)

Googling this issue you will get thousands of topics related to it.

Offline

#5 2010-03-06 10:29:51

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: [SOLVED] Mysql + Java (JDBC driver problem)

i did, but nothing help me, for now :S
ps. im not that stupid, i search before ask

Last edited by hBd (2010-03-06 10:30:35)

Offline

#6 2010-03-06 17:15:09

Gruntz
Member
From: Haskovo, Bulgaria
Registered: 2007-08-31
Posts: 256

Re: [SOLVED] Mysql + Java (JDBC driver problem)

Did someone found a solution to this issue. I have the same problem like in the 3# post here and I am looking for a solution now.

Can anyone give me a hint?

Offline

#7 2010-03-06 18:11:43

Loke
Member
From: Denmark
Registered: 2005-10-21
Posts: 139

Re: [SOLVED] Mysql + Java (JDBC driver problem)

I had a similar issue a while back - which I solved by commenting out "skip-networking" in my.cnf. Not sure if it will solve your issue as well, but you could give it a try.

(mysqld needs restart after the changes have been made, obviously.)

Last edited by Loke (2010-03-06 18:12:44)

Offline

#8 2010-03-06 18:38:17

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: [SOLVED] Mysql + Java (JDBC driver problem)

cat /etc/my.cnf | grep skip-networking
#skip-networking

should look like this, not ? not work... :S
the mysqld is up, and running. i can acces trought phpmyadmin...

Offline

#9 2010-03-06 19:29:02

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: [SOLVED] Mysql + Java (JDBC driver problem)

Finally it works now! The problem was the skip-networking option, it was commented at /etc/my.cnf but mysql use the /etc/mysql/my.cnf conf file, and there was not commented. I figure it out that 3306 port was not open,

sudo nmap -p 3306 localhost

Starting Nmap 5.21 ( http://nmap.org ) at 2010-03-06 21:18 EET
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000036s latency).
rDNS record for 127.0.0.1: localhost.localdomain
PORT     STATE  SERVICE
3306/tcp closed mysql

after i commented out, that line, and restarted the mysqld it works perfectly, and the java app to!

sudo nmap -p 3306 localhost

Starting Nmap 5.21 ( http://nmap.org ) at 2010-03-06 21:24 EET
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000041s latency).
rDNS record for 127.0.0.1: localhost.localdomain
PORT     STATE SERVICE
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds

hope that helps u guys to! i think the post can be renamed to SOLVED!

Offline

#10 2010-03-06 19:42:23

Loke
Member
From: Denmark
Registered: 2005-10-21
Posts: 139

Re: [SOLVED] Mysql + Java (JDBC driver problem)

I'm glad it helped smile

And yeah it's a while since the configuration was moved; http://www.archlinux.org/news/473/

Offline

#11 2010-03-06 19:46:03

Gruntz
Member
From: Haskovo, Bulgaria
Registered: 2007-08-31
Posts: 256

Re: [SOLVED] Mysql + Java (JDBC driver problem)

hBd, thank you man. That solvet my problem too. But i had it the other way. The network option was commented in /etc/mysql/my.cnf conf and not commented in /etc/my.cnf. I uncommented in both files and it worked.

Thank you.

Offline

Board footer

Powered by FluxBB