views:

42

answers:

5

I have my java application and try to connect with mysql database.But i can't able to get the output,i am getting the Exception error.I think i am not able to connect with the driver.My OS is Linux(Ubuntu).


nikki@nikki-laptop:~$ java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9) (6b20-1.9-0ubuntu1)
OpenJDK Client VM (build 17.0-b16, mixed mode, sharing)

nikki@nikki-laptop:~$ echo $JAVA_HOME
/usr/java/jdk1.6.0_20/bin/java


nikki@nikki-laptop:~$ echo $CLASSPATH
.:/usr/share/java/mysql.jar:/home/nikki/temp/src/jclass

My jdk path is /usr/lib/jvm/java-1.6.0-openjdk/jre


My Java program is

import java.sql.*;
class Query1
 {
 public static void main(String args[])throws Exception
 {
 try
 {  // the mysql driver string
  Class.forName("com.mysql.jdbc.Driver").newInstance ();
  // the mysql url = "jdbc:mysql://THE_HOST/THE_DATABASE";

  //String url = "sun.jdbc.odbc.JdbcOdbcDriver";

  String url ="jdbc:mysql://localhost/mylib_db";

  Connection conn = DriverManager.getConnection(url,"nikki","dkm007");
 Statement stmt = conn.createStatement();
 //ResultSet rs;
 ResultSet rs = stmt.executeQuery("select title from Book_dim where cost=435.89");
 while (rs.next())
 {
 String titlename = rs.getString("title");
 System.out.println(titlename + "\n");
 }
  conn.close();

 }
  catch(Exception e)
  {
  System.err.println("Got an exception! ");
                System.err.println(e.getMessage());
  }
 }
 }

My output is

-    -      -      -             -
nikki@nikki-laptop:~/Documents/Chinu/mydbP$ javac Query1.java
nikki@nikki-laptop:~/Documents/Chinu/mydbP$ java Query1
Got an exception! 
com.mysql.jdbc.Driver

I didn't added any driver...i don't know which Driver should i use .How can i add driver? If possible plz send me the reply.

A: 

Download the mysql jdbc driver and add it to your classpath.

Petar Minchev
i had already downloaded and it's in (/home/nikki/temp/src/jclass/mysql-connector-java-5.0.8).My jdk is in(/usr/lib/jvm/java-1.6.0-openjdk).But my CLASSPATH shows (bash: :/usr/share/java/mysql.jar:/home/nikki/temp/src/jclass: No such file or directory) My JAVA_HOME SHOWS (nikki@nikki-laptop:~$ $JAVA_HOMEbash: /usr/java/jdk1.6.0_20/bin/java: No such file or directory)Is it right?how can i set path?
+1  A: 

You need to add the Driver to the classpath of you application.

nikki@nikki-laptop:~/Documents/Chinu/mydbP$ java -cp /path/to/mysqldriver.jar Query1
Alexander Pogrebnyak
i do not know how to set path for connector-java-5.0.8 .My mysql- connector has been downloaded to (/home/nikki/temp/src/jclass/mysql-connector-java-5.0.8).My jdk has been in (/usr/lib/jvm/java-1.6.0-openjdk).If i am writing to show classpath it gives..........
i had already downloaded and it's in (/home/nikki/temp/src/jclass/mysql-connector-java-5.0.8).My jdk is in(/usr/lib/jvm/java-1.6.0-openjdk).But my CLASSPATH shows (bash: :/usr/share/java/mysql.jar:/home/nikki/temp/src/jclass: No such file or directory) My JAVA_HOME SHOWS (nikki@nikki-laptop:~$ $JAVA_HOMEbash: /usr/java/jdk1.6.0_20/bin/java: No such file or directory)Is it right?how can i set path?
@user490261. Please RTFM at http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing-classpath.html
Alexander Pogrebnyak
A: 

use code block to post code.

vishnu
where is code block?
+1  A: 

You likely haven't included the mysql driver on the classpath. Download the JDBC driver from mysql and put the mysql-connector-java-[version]-bin.jar file on your classpath.

It would also be helpful to know more about just what exception is being thrown. Here is how you do that:

catch(Exception e)
{
    e.printStackTrace();
}
Andrew
The OP does this, the output is : com.mysql.jdbc.Driver
KevinDTimm
No, that's e.getMessage(), which doesn't tell you the class of the exception. e.printStackTrace() does.
Andrew
@Andrew +1. Just add the printout of exception message as well. In situations like this you often need both.
Alexander Pogrebnyak
@Andrew - missed the distinction. In this case though, the error displayed described the error completely - no jdbc driver in the classpath (but your suggestion was/is helpful)
KevinDTimm
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at Query1.main(Query1.java:11)It shows the above errors.