我一直在尝试一个简单的程序来将MYSQL连接到我的Java应用程序。我正在使用Windows操作系统。我使用了在线教程并在记事本中尝试了以下程序:
import java.sql.*;
public class Database1{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
static final String USER = "root";
static final String PASS = "admin";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
System.out.println("ID NAME ");
while(rs.next()){
String id=rs.getString("ID");
String name=rs.getString("NAME");
System.out.print(id);
System.out.println(name);
}
rs.close();
stmt.close();
conn.close();
} //close try block
catch(SQLException s){ s.printStackTrace();}
catch(Exception e){e.printStackTrace();}
finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}
我收到了错误:
C:\JavaP>java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g Database1
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 Database1.main(Database1.java:17)
Goodbye!
经过数小时的谷歌搜索和在线搜索后,我终于能够解决我的问题了( - 在我的情况下,java lib文件夹(C:\ Program Files \ Java \ jdk1.8.0_05 \ lib)的位置是在环境变量中维护的,没有任何java程序可以执行。)
但现在我遇到了另一个问题:
C:\JavaP>java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g.jar Database1
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database1.main(Database1.java:17)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.Signature
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)
... 3 more
答案 0 :(得分:3)
您需要add
jar
包含课程org.aspectj.lang.Signature
的{{1}}文件的位置classpath
等。
java -cp .;C:\JavaP\mysql-connector-java-3.1.14-bin-g.jar;C:\JavaP\aspectj.jar Database1
答案 1 :(得分:0)
这里是JAR的清单:
Manifest-Version: 1.0
Name: org/aspectj/lang/
Specification-Title: AspectJ Runtime Classes
Specification-Version: 1.8
Specification-Vendor: aspectj.org
Implementation-Title: org.aspectj.tools
Implementation-Version: 1.8.7
Implementation-Vendor: aspectj.org
Bundle-Name: AspectJ Runtime
Bundle-Version: 1.8.7
Bundle-Copyright: (C) Copyright 1999-2001 Xerox Corporation, 2002 Palo
Alto Research Center, Incorporated (PARC), 2003-2009 Contributors.
All Rights Reserved.
答案 2 :(得分:0)
事实上,我认为你为jdbc驱动程序选择调试版jar,当我下载MySQL SQL driver jar并解压缩zip文件时,它看起来如下:
您应该在调试文件夹中选择名称为mysql-connector-java-5.0.8-bin.jar
但不是mysql-connector-java-5.0.8-bin**-g**.jar
的jar。