Solved

Mysql JDBC not connecting(Used connector/j)

Posted on 2009-07-13
18
521 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
ID: 24846371
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
ID: 24846375
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
ID: 24846381
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
ID: 24846388
I tried placing the jar in the same folder too. Even that didnt work
0
 
LVL 92

Expert Comment

by:objects
ID: 24846391
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
ID: 24846392
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
ID: 24846416
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
ID: 24846420
try pointing at mysql.jar
that way you can update without mucking around with classpath
0
 

Author Comment

by:rmvprasad
ID: 24846435
I dont have expertise in unix to that extent. Is there any possibility where I can be guided
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24847051
>>
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
ID: 24847058
> 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
ID: 24847112
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
ID: 24847132
good to see my comments are still getting repeated :)
though it is relevant
0
 

Author Comment

by:rmvprasad
ID: 24861010
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
ID: 24861735
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
ID: 24862628
>> 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
ID: 24865346
> 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
ID: 24865759
> :/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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 145
Error with Java/Cache JDBC Classpath 2 31
Java / Linux and Regular Expressions 11 70
thymeleaf natural templating vs JSP 2 67
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

911 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

20 Experts available now in Live!

Get 1:1 Help Now