Dear all,
I want to connect the my mysql database in the below java program.But i could not able to connect the myslq database.
i have downloaded the below jar file and put the jar file in the below path.

Jar File:
=========
mysql-connector-java-5.0.8-bin.jar


Path:
====
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/lib

mysql version
=============
SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.0.77 |
+-----------+


CODING
======

import java.sql.*;
public class testsql{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "password";
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
    } catch (Exception e) {
        System.out.println("cannot connect to database ");
  e.printStackTrace();
    }
  }
}

Error:
=====
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at testsql.main(testsql.java:13)

Can any one say what i missed in the above program.

I have compiled the program successfully .
But i could not able to run the program.


Thank you,

With Regards,
Prem

Have you put the mysql-connector-java-5.0.8-bin.jar at the classpath ?

Yes i have put the jar file in the path and put the 666 permission to that jar file.

PATH:
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/lib


[root@C0210217 lib]# echo $JAVA_HOME
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/

I am using linux redhat system.

Thank you for your response

No, that is not "putting it on the classpath". Do not put anything into the jre/jdk directories. If executing/compiling from the command line (without the -jar option) use the -cp option, if running from the command line with the -jar option, correctly configure the manifest file and place the jar in the proper place relative to the application jar, if using a web container/app server place the jar in the proper place (i.e. normally WEB-INF/lib for web containers but for drivers possibly a shared/lib somewhere in the server libs), and if using an IDE then add the jar as a library to the projects properties/preferences. See the tools documentation for the java/javac commands, the Manifest File tutorial, the web container/app server documentation, and the IDE documentation for more details on the chosen path.

Dear masijade,

I have specify the classpath to my program.Still i did not able to run the program.

Syntax:
========
javac -classpath /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/lib/mysql-connector-java-5.0.8-bin.jar testsql.java


Error:
======
MySQL Connect Example.
cannot connect to database
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at testsql.main(testsql.java:13)


Thank you,

With Regards,
Prem.

You have posted the javac command which you are using to compile your code. What is the java command you are using to execute your code? Are you properly setting the classpath when spawning your Java process?

Also, as already mentioned, move that mysqlconnection jar out of the JDK lib directory.

Dear all,

I have removed mysql-connector-java-5.0.8-bin.jar from the lib directory.
And placed the mysql connector jar in some other directory.

javac -classpath /home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar testsql.java

When i compile it .There is no errors.

When i run the program by using the below command

java testsql

I get the above errors .Still not getting the result.

Thank you for your reply.

Dear all,

I have removed mysql-connector-java-5.0.8-bin.jar from the lib directory.
And placed the mysql connector jar in some other directory.

javac -classpath /home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar testsql.java

When i compile it .There is no errors.

When i run the program by using the below command

java testsql

I get the above errors .Still not getting the result.

Thank you for your reply.

As ~S.O.S~ said, you need to set the classpath when you execute the class file

Dear javaaddict,

I have set the classpath still not getting the result

set classpath=/home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar

or

java -classpath /home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar testsql

I have received the below error message

Exception in thread "main" java.lang.NoClassDefFoundError: testsql
Caused by: java.lang.ClassNotFoundException: testsql
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)


Did i set the classpath correctly ?

I found testsql.class in my java folder.Even though i am receiving classnot found error message.

Thank you,

With Regards,
Prem

Which directory are you executing this command from? Is your class inside any package? Try: java -classpath .:/home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar testsql

Dear all,

When i execute the below line i received the below errors.No i am not using package .My class file is located in the javaprograms folder.

java -classpath .:/home/prem/javaprograms/mysql-connector-java-5.0.8-bin.jar testsql
MySQL Connect Example.
cannot connect to database
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused

STACKTRACE:

java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:381)
at java.net.Socket.connect(Socket.java:537)
at java.net.Socket.connect(Socket.java:487)
at java.net.Socket.<init>(Socket.java:384)
at java.net.Socket.<init>(Socket.java:227)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at testsql.main(testsql.java:14)


** END NESTED EXCEPTION **

Last packet sent to the server was 1 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at testsql.main(testsql.java:14)


Thank you,

With Regards,
Prem

Thanks to all,
My program is executed successfully.Mysql service is stopeed so i get the bug .Now i started the mysql the program works fine.Thanks to all .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.