Solved

Mysql JDBC not connecting(Used connector/j)

Posted on 2009-07-13
18
528 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 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