我有問題,嘗試使用下面的代碼來獲取數據庫連接:如何使用OpenJDK的連接到MySQL在Linux Mint的
import java.lang.*;
import java.sql.*;
public class Demo {
public static void main(String[] args){
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?" + "user=test&password=123456");
}catch(SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}catch(Exception ex){
System.out.println("Exception: " + ex.getMessage());
}
}
}
所輸出的錯誤信息是:
SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/test?user=test&password=123456
SQLState: 08001
VendorError: 0
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 Demo.main(Demo.java:9)
我的Java版本如下:
java -> /usr/lib/jvm/java-6-openjdk-i386/bin/java
javac -> /usr/lib/jvm/java-6-openjdk-i386/bin/javac
javac.1.gz -> /usr/lib/jvm/java-6-openjdk-i386/man/man1/javac.1.gz
javadoc -> /usr/lib/jvm/java-6-openjdk-i386/bin/javadoc
javadoc.1.gz -> /usr/lib/jvm/java-6-openjdk-i386/man/man1/javadoc.1.gz
javah -> /usr/lib/jvm/java-6-openjdk-i386/bin/javah
javah.1.gz -> /usr/lib/jvm/java-6-openjdk-i386/man/man1/javah.1.gz
javap -> /usr/lib/jvm/java-6-openjdk-i386/bin/javap
javap.1.gz -> /usr/lib/jvm/java-6-openjdk-i386/man/man1/javap.1.gz
javaws -> /usr/lib/jvm/java-6-openjdk-i386/jre/bin/javaws
javaws.1.gz -> /usr/lib/jvm/java-6-openjdk-i386/jre/man/man1/javaws.1.gz
我從字面上不知道如何解決此錯誤消息。數據庫存在。用戶名和密碼存在。我目前沒有添加任何表到數據庫,但我不認爲這可能是問題,因爲我只做了一個連接......
驅動程序安裝在/ usr/share/java下
-rw-r--r-- 1 root root 822524 10月 20 2011 mysql-connector-java-5.1.16.jar
-rw-r--r-- 1 root root 827942 9月 9 22:40 mysql-connector-java-5.1.21-bin.jar
lrwxrwxrwx 1 root root 35 9月 9 22:40 mysql-connector-java.jar -> mysql-connector-java-5.1.21-bin.jar
lrwxrwxrwx 1 root root 24 10月 20 2011 mysql.jar -> mysql-connector-java.jar
有誰知道如何解決它.. 感謝您的幫助! :)
我安裝驅動程序,但仍然有問題.. 驅動程序是在/ usr/share/java目錄/ 'code' 使用mysql-connector-java的5.1.16.jar 使用mysql-connector-Java的5.1.21-bin.jar mysql-connector-java.jar - > mysql-connector-java-5.1.21-bin.jar mysql.jar - > mysql-connector-java.jar –
您是否正在使用任何種類的IDE如eclipse或netbeans? – curiouss
不,jsut純文本編輯器像Sublime Text 2 –