我想連接到數據庫。早期它工作正常。但現在有些罐子已經被刪除或者我不知道的東西(紅十字會在他們身上)。但我再次下載了所有jar(mysql-connector,commons-io-2.4),但我仍然在Class.forName(「com.mysql.jdbc.Driver」)上得到了classnotfound異常; 這裏是我的代碼越來越java.lang.ClassNotFoundException:在java中的com.mysql.jdbc.Driver
package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC_oracle {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//step1 load the driver class
Class.forName("com.mysql.jdbc.Driver");
//step2 create the connection object
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
//step3 create the statement object
Statement statement = connection.createStatement();
statement.executeUpdate("sql_query");
ResultSet rs=statement.executeQuery("sql_query");
while(rs.next())
System.out.println("I am triyng to print data");
//step5 close the connection object
connection.close();
}
}
這是錯誤
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at deiniteguide.JDBC_example.main(JDBC_example.java:24)
我一直在使用嘗試捕捉也是它不是爲我工作嘗試。
mysql-connector.jar文件不在類路徑中。無關,但'Class.forName()'不再需要了。只要它們在類路徑上,JDBC驅動程序就會自動註冊。 – Andreas