【问题标题】:Connect to SQL_database with java [duplicate]使用 java 连接到 SQL_database [重复]
【发布时间】:2017-05-04 13:46:46
【问题描述】:

我希望在 MySQL 和我的 Java 应用程序之间建立连接。 当我运行程序时,我得到这个错误:我的日志信息是真的。这是一个我应该能够在我的本地数据库中添加、插入、删除和更新客户信息的项目。

java.lang.ClassNotFoundException: com/mysql/jdbc/Driver.class
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at sample.Database.Connection.getConnection(Connection.java:26)
    at sample.Presentation.Controller.<init>(Controller.java:19)
    at sample.Presentation.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

代码

public class Controller {

public Controller(Stage primaryStage) {
    ButtonPane buttonPane = new ButtonPane();
    AddPane addPane = new AddPane();
    VBox vBoxStart = new VBox();
    Connection connection = new Connection();

    try {
        connection = connection.getConnection();
    }catch (Exception e){
        System.out.println("No connection");
    }
    Querys querys = new Querys();


    HBox hBoxOption = new HBox(buttonPane.ButtonPane(),addPane.getPane());// Menu and add/update/delete
    HBox hBoxView = new HBox();  // Calender/tableView

    vBoxStart.getChildren().addAll(hBoxOption,hBoxView);

    Scene scene = new Scene(vBoxStart,1000,1000);
    primaryStage.setScene(scene);
    primaryStage.show();

    Connection finalConnection = connection;
    addPane.getButtonAddCustomer().setOnAction(event ->{
        querys.viewTable(finalConnection,"customer");
    } );

}
}
public class Connection {

String userName = "root";
String password = "rasmus12";
String dbms = "mysql";
String serverName = "localhost";
String portNumber = "3306";

public Connection getConnection()  {

    Connection conn = null;
        try {
            Class.forName("com/mysql/jdbc/Driver.class");
            conn = (Connection) DriverManager.getConnection(
                    "jdbc:" + this.dbms + "://" +
                            this.serverName +
                            ":" + this.portNumber + "/",
                    this.userName,this.password);
            System.out.println("Connected to database");
        } catch (SQLException e) {
            System.out.println("SQL Exception in connection");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    return conn;
}
}

public class Querys {

public void viewTable(Connection con, String dbName) {


    Statement stmt = null;
    String query = "select * " +
            "from " + dbName;
    try {

        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            int id = rs.getInt("person_id");
            String firstName = rs.getString("person_first_name");
            String lastName = rs.getString("person_last_name");
            String adresse = rs.getString("person_adresse");
            String zip = rs.getString("person_zip");
            String city = rs.getString("person_city");
            int mobilePhone = rs.getInt("person_mobile_phone");
            int phone = rs.getInt("person_phone");
            int driverLicenceNumber = rs.getInt("person_driver_licence_number");
            String driverSinceDate = rs.getString("person_driver_since_date");
            String registrationNumber = rs.getString("person_registration_number");
            int orderId = rs.getInt("order_id");
            System.out.println(id + "\t" + firstName +
                    "\t" + lastName + "\t" + adresse +
                    "\t" + zip +
                    "\t" + city +
                    "\t" + mobilePhone +
                    "\t" + phone +
                    "\t" + driverLicenceNumber +
                    "\t" + driverSinceDate +
                    "\t" + registrationNumber+
                    "\t" + orderId);
        }
    } catch (SQLException e) {
        System.out.println("SQL Exception");
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
}

【问题讨论】:

  • 我已经删除了 IntelliJ 标签,因为您的问题实际上与 IntelliJ 无关。您使用 IntelliJ 开发程序与此问题无关。鉴于此问题是由拼写错误/错误的类名引起的(请参阅answer by YCF_L),我决定将您的问题关闭为从 Java 连接 MySQL 的规范副本。

标签: java mysql jdbc


【解决方案1】:

驱动不正确,你必须使用:

Class.forName("com.mysql.jdbc.Driver");

而不是这个:

Class.forName("com/mysql/jdbc/Driver.class");

但自 Java 6 起不再需要调用 Class.forName(),您可以阅读 this,并像 @duffymo 在他的回答中提到的那样,确保驱动程序 jar 存在于您的类路径中

【讨论】:

    【解决方案2】:

    ClassNotFoundException 表示类加载器找不到 MySQL JDBC 驱动类。

    您需要下载 MySQL 连接器 JAR 并将其添加到您的 CLASSPATH。

    我推荐MySQL JDBC tutorial

    这里有一个 Java 提示:不要在 catch 块中编写消息:

    catch (SQLException e) {
        System.out.println("SQL Exception");
    }
    

    打印或记录整个堆栈跟踪。这是您的消息提供的更多信息。

    catch (SQLException e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 好的,已经完成了,我不再收到错误,但是当我运行程序时,sqlException 正在连接内。
    • 发布异常。这将帮助我们找出您的下一个错误是什么。
    • 我正在尝试使用此代码,但它说我需要创建类 JDBCTutorialUtilities,catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); }
    • 我不明白。你不需要任何课程来做我建议的事情。
    • 我没有得到任何 expcetion 只是说没有连接:/
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2017-11-15
    • 2012-12-15
    • 2013-05-05
    • 2019-08-20
    • 2020-01-28
    • 2012-10-20
    相关资源
    最近更新 更多