我有一个Maven项目正在运行,当我尝试运行GET调用时,它会向控制台输出一个ClassNotFound异常,然后在页面上给我一个null结果。我有一个动态的Web项目,我从中复制/粘贴代码,它可以正常工作。我不知道如何调试它,因为它看起来应该可以工作。
这是错误:
Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver0:0:0:0:0:0:0:1 - - [18/Jul/2013:21:35:19 +0000] "GET /parties HTTP/1.1" 200 87 163 163
0:0:0:0:0:0:0:1 - - [18/Jul/2013:21:35:19 +0000] "GET /favicon.ico HTTP/1.1" 404 1329 9 9
以下是代码发生的地方:
package core;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.HashMap;
public class DBConnection {
private Connection con;
private static Statement statement;
private static ResultSet resultSet;
public static DBConnection connection;
private static ResultSetMetaData meta;
private static HashMap<String,Party> map;
public Party party;
private DBConnection()
{
try
{
map = new HashMap<String,Party>();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://atom3.cisco.com:3306/asdf", "asdf",
"asdf");
statement = con.createStatement();
readData();
}
catch (Exception e)
{
System.out.print("Error: "+e);
}
}
public void readData()
{
try
{
map.clear();
String query = "(SELECT * FROM PureServlet)";
resultSet = statement.executeQuery(query);
meta = resultSet.getMetaData();
String columnName, value, partyName;
while(resultSet.next())
{
partyName = resultSet.getString("PARTY_NAME");
map.put(partyName, new Party()); //this is the map that keeps track of all parties
party = map.get(partyName);
for(int j=1;j<=meta.getColumnCount();j++) //necessary to start at j=1 because of MySQL index starting at 1
{
columnName = meta.getColumnLabel(j);
value = resultSet.getString(columnName);
party.getPartyInfo().put(columnName, value); //this is the hashmap within the party that keeps
//track of the individual values. The column Name = label, value is the value
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
public static HashMap<String,Party> getPartyCollection()
{
if(connection == null)
{
connection = new DBConnection();
}
return map;
}
}
答案 0 :(得分:3)
需要添加依赖项。我发誓我以前加了它,一定是不小心删掉了它。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
答案 1 :(得分:0)
lib目录中缺少JAR文件(ojdbc6.jar) 下载并将其放在lib目录中,然后重试。