?
Solved

Mysql JDBC not connecting(Used connector/j)

Posted on 2009-07-13
18
Medium Priority
?
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 500 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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