【问题标题】:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Vertrigo server eclipse [duplicate]java.lang.ClassNotFoundException:com.mysql.jdbc.Driver Vertrigo 服务器 Eclipse [重复]
【发布时间】:2013-06-12 07:32:13
【问题描述】:

我正在尝试连接到我的 Windows 7 电脑上的 mysql 数据库(作为 Vertrigo 服务器的一部分),但它不断向我抛出“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”。

以下是我的代码:

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;

公共类 ShippingCompany {

private Connection conn;
private Statement stmt;
private List<Journey> journeyList;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://172.16.127.38:3306/testRestricted";

public ShippingCompany(String dbUser, String dbPassword) throws Exception {
    journeyList = new ArrayList<Journey>();
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(DB_URL, dbUser, dbPassword);
            stmt = conn.createStatement();
        } catch (SQLException e) {
            System.out.println("SQL Exception:" + e);
        }
}

public List<String> readAllPorts() throws SQLException{
     List<String> allPorts = new ArrayList<String>();
        String command = "SELECT * FROM Ports";
        ResultSet rs = stmt.executeQuery(command);
        while (rs.next()) {  // database name string is in first column of result set
            allPorts.add(rs.getString(2));
            //In command concole, print out all ports
            System.out.println(rs.getString(2));
        }
        return allPorts;
}

public List<Journey> getAllJourneys(String startPort, String startDate, String endPort){
    return new LinkedList<Journey>();
}

private void findPaths(Journey journey, String endPort){

}

public void Close(){

}

public static void main(String[] args) throws Exception{
    //TODO : entry point of the program
    ShippingCompany sc = new ShippingCompany("root", "vertrigo");
    sc.readAllPorts();
}

} 应该是

【问题讨论】:

  • 这么多dup,你怎么选?

标签: java mysql


【解决方案1】:

运行程序时,您的类路径中需要有 mysql 连接器 jar。如果您通过命令行运行它,那么您可以执行以下操作:

java -cp  <.;path to sql jar> ShippingCompany

【讨论】:

  • 谢谢,我已经尝试通过eclipse导入功能添加jar文件,我已经将jar文件添加到bin和src,我仍然得到这个错误。有什么想法吗?再次感谢。
  • @Jeffrey 从您的屏幕截图中,您似乎正在尝试导入源而不是库。您需要在eclipse中添加这样的jar:项目->属性->构建路径->添加jar/添加外部jar(取决于jar是在您的项目中还是在外部)
  • 感谢 Juned Ahsan,它运行良好。
猜你喜欢
  • 1970-01-01
  • 2013-03-27
  • 2011-12-27
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多