我与BlueJ的一个新的程序员,我不得不创建一个名为Connexion
类连接到我的MySQL数据库,但我得到这个错误:如何解决“抛出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver”与BlueJ的和Wampserver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
我的代码:
import java.sql.* ;
public class Connexion{
/**
* Connect to MySQL and read the table "Etat", then
* print the contents of the first column.
*/
public static void test()
{
try
{
// Load the database driver
Class.forName("com.mysql.jdbc.Driver") ;
// Get a connection to the database
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/OACA?user=root&password=") ;
// Print all warnings
for(SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning())
{
System.out.println("SQL Warning:") ;
System.out.println("State : " + warn.getSQLState() ) ;
System.out.println("Message: " + warn.getMessage() ) ;
System.out.println("Error : " + warn.getErrorCode()) ;
}
// Get a statement from the connection
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery("SELECT * FROM Etat") ;
// 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)
{
System.out.println(e) ;
}
}
public static void main(String[] args) {
test();
}
}
那么,如何连接到我的Wampserver中的MySQL v5.6.17数据库。 我认为我需要一个coonector.jar但我不知道我会真正需要哪一个。 你能给出你的意见吗?
可能重复:http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse –