Solved

Mysql JDBC not connecting(Used connector/j)

Posted on 2009-07-13
18
523 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expectj telnet failing 5 35
arguments to jar 5 26
eclipse apache tomcat admin console 52 96
how to install java on RHEL image on EC2 4 26
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

776 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