i need the help for connecting mysql with java in ubuntu OS
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";
Connec开发者_StackOverflow社区tion 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.
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
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();
}
Check these references out:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-examples.html
http://dev.mysql.com/downloads/connector/j/
Download the mysql jdbc driver and add it to your classpath.
精彩评论