1

我正在尝试在我的 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();
}

} 在此处输入图像描述 应该是

在此处输入图像描述

4

1 回答 1

1

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

java -cp  <.;path to sql jar> ShippingCompany
于 2013-06-16T00:31:09.603 回答