【发布时间】:2012-02-03 11:25:55
【问题描述】:
这个问题在这里被问了很多,但我仍然无法解决我的问题:
我将mysql-connector-java-5.1.18-bin 放入C:\Program Files\Java\jre6\lib\ext 文件夹。
我有这个代码:
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a connection to the database
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql", "root", "4958ps");
// Get a statement from the connection
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
// Loop through the result set
while( rs.next() )
System.out.println( rs.getString(1) ) ;
// Close the result set, statement and the connection
rs.close() ;
stmt.close() ;
conn.close() ;
} catch( SQLException se ) {
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( se != null ) {
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
} catch( Exception e ) {
e.printStackTrace();
}
我得到一个带有以下堆栈跟踪的 ClassNotFoundException:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at pack.Test.main(Test.java:14)
我还将CLASSPATH 变量更改为C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.18-bin
有什么想法吗?
【问题讨论】:
-
您是否为此使用 Eclipse?您是在编写批处理脚本还是 shell 脚本?
-
@iowatiger08:我正在使用 NetBeans
-
你把mysql-connector jar 文件放在哪里了?它在类路径上吗?
-
@mcfinnigan:我把它放在
C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.18-bin -
不应该 - 除非 NetBeans 没有使用该 JRE。单击您的项目并转到属性-> 库-> 管理平台。检查它默认使用的 JDK,并确保 mysql jar 显示在 Classes 下。
标签: java jdbc classnotfoundexception