可能重複:
ClassNotFoundException com.mysql.jdbc.DriverJava的MySQL的:ClassNotFoundException的 - >找不到驅動程序
我已經包括mysql-connector-java-5.1.16-bin.jar
到我的Eclipse庫,這個代碼是基於我有教程因爲我之前沒有在Java中使用過MySQL,所以在網上找到它。不幸的是我沒有看到,我使用acutual mysql-connector-java-5.1.16-bin.jar
和控制檯打印Could not find driver.
意味着ClassNotFoundException
被拋出。你有沒有看到我看不到的(可能是顯而易見的)問題?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;
public class MysqlConnector {
private static MysqlConnector instance = null;
private static Connection conn = null;
private static String dbHost = "localhost";
private static String dbPort = "3306";
private static String database = "sample";
private static String dbUser = "root";
private static String dbPassword = "";
public MysqlConnector() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":"
+ dbPort + "/" + database + "?" + "user=" + dbUser + "&"
+ "password=" + dbPassword);
} catch (ClassNotFoundException e) {
System.out.println("Could not find driver."); //TODO LOGGER
} catch (SQLException e) {
System.out.println("Could not connect to database."); //TODO LOGGER
}
}
public static MysqlConnector getInstance()
{
if(instance == null)
instance = new MysqlConnector();
return instance;
}
public boolean validateApiKey(String apikey)
{
if(conn != null)
{
Statement query;
try {
query = conn.createStatement();
String sql = "SELECT startdate, expiration, active " + "FROM apikeys "
+ "WHERE apikey = '" + apikey +"'";
ResultSet result = query.executeQuery(sql);
if(result.getFetchSize()>0 && result.getInt("active")==1){
Date now = new Date();
Date startdate = result.getDate("startdate");
Date expirationdate = result.getDate("expiration");
if(now.before(expirationdate) && now.after(startdate)){
return true;
} else {
return false;
}
} else {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
return false;
}
}
這是一個幾乎可以肯定的類路徑問題。看看非常相似票 – 2011-06-01 15:42:31
可能重複的是不是蝕。所以它不是一個確切的副本。 – Bozho 2011-06-01 15:43:56
我不確定是否同意這是一個確切的副本,至少在這裏我找到了一個我無法在其他票證中找到的答案。 – 2011-06-01 16:01:55