我只是从连接性开始,并尝试了这个示例。我已经安装了必要的软件。还将jar文件复制到/ext folder.Yet中,下面的代码具有以下错误
import java.sql.*;
public class Jdbc00 {
public static void main(String args[]){
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url =
"jdbc:mysql://localhost:3306/mysql"
DriverManager.getConnection(url,"root", "root");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//Get a Statement object
stmt = con.createStatement();
//Create the new database
stmt.executeUpdate(
"CREATE DATABASE JunkDB");
stmt.executeUpdate(
"GRANT SELECT,INSERT,UPDATE,DELETE," +
"CREATE,DROP " +
"ON JunkDB.* TO 'auser'@'localhost' " +
"IDENTIFIED BY 'drowssap';");
con.close();
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end main
}//end class Jdbc00但它给出了以下错误
D:\Java12\Explore>java Jdbc00
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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 Jdbc00.main(Jdbc00.java:11)有人能指导我纠正这个错误吗?
发布于 2010-05-15 16:11:59
在运行应用程序时,在类路径中找不到包含MySQL驱动程序类(com.mysql.jdbc.Driver)的jar文件。这就是ClassNotFoundException所抱怨的。
您需要将其添加到CLASSPATH环境变量中,或者在运行Java时使用类路径选项。例如:
java -cp mysql-connector-java-5.0.8-bin.jar Jdbc00使用您正在使用的任何MySQL连接器jar文件的名称和位置。(如果您还没有在本地主机上安装MySQL,那么您的应用程序需要连接一些东西,您可能也需要这样做。)
发布于 2010-05-15 17:19:05
正如@Ash所说,问题在于连接器/J驱动程序不在您的类路径中。您可以从this page下载最新版本(5.0.12)。
https://stackoverflow.com/questions/2839391
复制相似问题