Solved

Mysql JDBC not connecting(Used connector/j)

Posted on 2009-07-13
18
517 Views
Last Modified: 2013-12-29
I have been trying to connect to mysql database using jdbc code. I have done this before. When I came across with the same issue i added the connector/j jar to the classpath and it worked. Now I have done the same still it is not connecting. Below is the class path. I have this placed in .bashrc so it can CLASSPATH can be set at boot time itself. I created a folder named temp and flaced the following code. It is giving the error as below. I have done this many time before. I am not sure why I am not getting it now.

.bashrc:
CLASSPATH=$CLASSPATH:/use/share/java/mysql-connector-java-5.1.6-bin.jar
export CLASSPATH

prasad@prasad-laptop:~$ echo $CLASSPATH
:/use/share/java/mysql-connector-java-5.1.6-bin.jar

Error:
prasad@prasad-laptop:~/temp$ java MysqlTest
Copyright 2004, R.G.Baldwin
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:164)
      at MysqlTest.main(MysqlTest.java:10)

import java.sql.*;
 
 

public class MysqlTest {

  public static void main(String args[]){

    System.out.println(

                  "Copyright 2004, R.G.Baldwin");

    try {

      Statement stmt;

      Class.forName("com.mysql.jdbc.Driver").newInstance();

      String url =

            "jdbc:mysql://localhost:3306/mysql";

      Connection con =

                     DriverManager.getConnection(

                                 url,"root", "venkat7");

      System.out.println("URL: " + url);

      System.out.println("Connection: " + con);

      stmt = con.createStatement();

   con.close();

    }catch( Exception e ) {

      e.printStackTrace();

    }//end catch

  }//end main

}//end class Jdbc11

Open in new window

0
Comment
Question by:rmvprasad
  • 7
  • 7
  • 3
  • +1
18 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
you can check the jar contents using

jar tvf /use/share/java/mysql-connector-java-5.1.6-bin.jar
0
 
LVL 20

Expert Comment

by:a_b
Comment Utility
Try placing the jar in the same directory as the class file that you are executing and then set the classpath and see.
0
 

Author Comment

by:rmvprasad
Comment Utility
It says

java.io.FileNotFoundException: /use/share/java/mysql-connector-java-5.1.6-bin.jar (No such file or directory)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.java:106)
      at java.io.FileInputStream.<init>(FileInputStream.java:66)
      at sun.tools.jar.Main.run(Main.java:184)
      at sun.tools.jar.Main.main(Main.java:903)
 when I went to that dir I am finding it
prasad@prasad-laptop:~$ cd /usr/share/java
prasad@prasad-laptop:/usr/share/java$ ls
libintl.jar      mysql-connector-java-5.1.5.jar      mysql-connector-java.jar
mysql-5.1.5.jar  mysql-connector-java-5.1.6-bin.jar  mysql.jar
0
 

Author Comment

by:rmvprasad
Comment Utility
I tried placing the jar in the same folder too. Even that didnt work
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
sounds like that file is a link pointing at nothing

ls -l  /use/share/java/mysql-connector-java-5.1.6-bin.jar
0
 

Author Comment

by:rmvprasad
Comment Utility
This is the output I got when I tried ls -lrt

prasad@prasad-laptop:/usr/share/java$ ls -lrt
total 1344
-rw-r--r-- 1 root root 655620 2007-12-04 07:55 mysql-connector-java-5.1.5.jar
-rw-r--r-- 1 root root   2493 2007-12-13 01:22 libintl.jar
-rw-r--r-- 1 root root 703265 2009-07-13 18:22 mysql-connector-java-5.1.6-bin.jar
lrwxrwxrwx 1 root root     15 2009-07-14 00:29 mysql.jar -> mysql-5.1.5.jar
lrwxrwxrwx 1 root root     30 2009-07-14 00:29 mysql-connector-java.jar -> mysql-connector-java-5.1.5.jar
lrwxrwxrwx 1 root root     30 2009-07-14 00:29 mysql-5.1.5.jar -> mysql-connector-java-5.1.5.jar
0
 

Author Comment

by:rmvprasad
Comment Utility
earlier I downloaded a different version and later updated to a different version. Probably that could be the reason, if I am not mistaken. How should I rectify this now and make mysql work
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
try pointing at mysql.jar
that way you can update without mucking around with classpath
0
 

Author Comment

by:rmvprasad
Comment Utility
I dont have expertise in unix to that extent. Is there any possibility where I can be guided
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>
CLASSPATH=$CLASSPATH:/use/share/java/mysql-connector-java-5.1.6-bin.jar
export CLASSPATH
>>

should probably be

CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.6-bin.jar
export CLASSPATH
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
> I dont have expertise in unix to that extent. Is there any possibility where I can be guided

 should be:

CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar

then when you update version

cd /usr/share/java
rm mysql.jar
ln -s mysql-connector-java-x.y.z-bin.jar mysql.jar

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The symlink is not really relevant - the problem is the typo in your classpath. But if you want to use the symlink, you would do the following:
ln -svf <PATH TO>/mysql-connector-java-x.y.z-bin.jar <PATH TO>/mysql.jar

Open in new window

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
good to see my comments are still getting repeated :)
though it is relevant
0
 

Author Comment

by:rmvprasad
Comment Utility
Sorry for the break I couldnt follow up with you guys yesterday.
1. There is no Mysql.jar file existing in my system.
2. I installed mysql in ubuntu OS using the command
   sudo apt-get install mysql-server
3. I downloaded mysql-connector-java-5.1.6-bin.jar file to tthe location /usr/share/java/mysql-connector-java-5.1.6-bin.jar
4. I changed the classpath to
CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar --> according to me is not right as there is no jar file existing
5. Then I made a link ln -s mysql-connector-java-x.y.z-bin.jar mysql.jar
Still the same issue
0
 

Author Comment

by:rmvprasad
Comment Utility
An intresting situation here
I visited the directory where jar file is located and typed the following command

 java -cp mysql-connector-java-3.1.14.jar com.mysql.jdbc.Driver
Exception in thread "main" java.lang.NoSuchMethodError: main --> here it dosent say class not found, but it still says "Exception in thresd main"
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>> java -cp mysql-connector-java-3.1.14.jar com.mysql.jdbc.Driver

is a command to run an application. The driver is not an application. As you correctly say, the class is found
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 1. There is no Mysql.jar file existing in my system.

there is, you posted it earlier :)
>>lrwxrwxrwx 1 root root     15 2009-07-14 00:29 mysql.jar -> mysql-5.1.5.jar

Its a link to the current version

> 3. I downloaded mysql-connector-java-5.1.6-bin.jar file to tthe location /usr/share/java/mysql-connector-java-5.1.6-bin.jar

better to let apt look after it

> Exception in thread "main" java.lang.NoSuchMethodError: main --> here it dosent say class not found, but it still says "Exception in thresd main"

thats because the driver does not contain a main which is entry point used by the application
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> :/use/share/java/mysql-connector-java-5.1.6-bin.jar

also get rid of that : from the CLASSPATH

though I would suggest getting rid of the CLASSPATH environment var altogether, it will only cause you problems.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now