我有我的 java 应用程序并尝试连接 mysql 数据库。但我无法获得输出,我收到异常错误。我想我无法连接驱动程序。我的操作系统是 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
我的jdk路径是/usr/lib/jvm/java-1.6.0-openjdk/jre
我的Java程序是
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());
}
}
}
我的输出是
- - - - -
nikki@nikki-laptop:~/Documents/Chinu/mydbP$ javac Query1.java
nikki@nikki-laptop:~/Documents/Chinu/mydbP$ java Query1
Got an exception!
com.mysql.jdbc.Driver
我没有添加任何驱动程序...我不知道应该使用哪个驱动程序。如何添加驱动程序? 如果可能请给我回复。
最佳答案
您需要将驱动程序添加到应用程序的类路径中。
nikki@nikki-laptop:~/Documents/Chinu/mydbP$ java -cp /path/to/mysqldriver.jar Query1
关于java - 我需要在 ubuntu 操作系统中将 mysql 与 java 连接的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4044898/