'在应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException',同时从数据库读取和显示 [英] ' Exception in Application start method, java.lang.reflect.InvocationTargetException ' while reading and displaying from database

查看:2548
本文介绍了'在应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException',同时从数据库读取和显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从MySQL数据库读取值,并将其显示在JavaFX中的表中。我使用netbeans IDE。当我运行我的代码,我有标题中提到的异常。我将在下面发布代码:

Am in attempt to read values from MySQL database and display it in a table in JavaFX. I use netbeans IDE. When I run my code I got the exception mentioned in the title. I will post code below :

public class ViewSubject extends Application {

private final TableView<Subject> table = new TableView<>();
private final ObservableList<Subject> data
        = FXCollections.observableArrayList();
final HBox hb = new HBox();

private Connection connect = null;
private Statement statement = null;
private final PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws ClassNotFoundException, SQLException {

    Scene scene = new Scene(new Group());
    stage.setTitle("Add Subject");
    stage.setWidth(650);
    stage.setHeight(550);
    stage.setResizable(false);

    final Label label = new Label("Subject Details");
    label.setFont(new Font("Calibri", 20));

    TableColumn sub = new TableColumn("Subject Name");
    sub.setMinWidth(350);
    sub.setCellValueFactory(
            new PropertyValueFactory<Subject, String>("subName"));
    sub.setCellFactory(TextFieldTableCell.forTableColumn());
    sub.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Subject, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Subject, String> t) {
                    ((Subject) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())).setSubName(t.getNewValue());
                }
            }
    );

    TableColumn code = new TableColumn("Subject Code");
    code.setMinWidth(130);
    code.setCellValueFactory(
            new PropertyValueFactory<Subject, String>("subCode"));
    code.setCellFactory(TextFieldTableCell.forTableColumn());
    code.setCellFactory(TextFieldTableCell.forTableColumn());
    code.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Subject, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Subject, String> t) {
                    ((Subject) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())).setSubCode(t.getNewValue());
                }
            }
    );

    TableColumn rev = new TableColumn("Revision");
    rev.setMinWidth(130);
    rev.setCellValueFactory(
            new PropertyValueFactory<Subject, String>("subRev"));
    rev.setCellFactory(TextFieldTableCell.forTableColumn());
    rev.setCellFactory(TextFieldTableCell.forTableColumn());
    rev.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<Subject, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<Subject, String> t) {
                    ((Subject) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())).setSubCode(t.getNewValue());
                }
            }
    );

    table.setItems(data);
    table.getColumns().addAll(sub, code, rev);

    final VBox vbox = new VBox();
    vbox.setSpacing(10);
    vbox.setPadding(new Insets(20, 20, 20, 20));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/project?"
                        + "user=root&password=virus");
        statement = connect.createStatement();

        resultSet = statement
                .executeQuery("select * from subject");
        writeResultSet(resultSet);
    } catch (ClassNotFoundException | SQLException e) {
        throw e;
    } finally {
        close();
    }

}

private void writeResultSet(ResultSet resultSet) throws SQLException {

    while (resultSet.next()) {

        String subname = resultSet.getString("subname");
        String code = resultSet.getString("subcode");
        String rev = resultSet.getString("subrev");

        data.add(new Subject(subname, code, rev));
    }
}

private void close() {
    try {
        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connect != null) {
            connect.close();
        }
    } catch (SQLException e) {

    }
}
}

包中还有一个类 -

There is one more class in the package -

public class Subject {

    private final SimpleStringProperty sub;
    private final SimpleStringProperty code;
    private final SimpleStringProperty rev;

    Subject(String subName, String subCode, String subRev) {
        this.sub = new SimpleStringProperty(subName);
        this.code = new SimpleStringProperty(subCode);
        this.rev = new SimpleStringProperty(subRev);
    }

    public String getSubName() {
        return sub.get();
    }

    public void setSubName(String subName) {
        sub.set(subName);
    }

    public String getSubCode() {
        return code.get();
    }

    public void setSubCode(String subCode) {
        code.set(subCode);
    }

    public String getSubRev() {
        return rev.get();
    }

    public void setSubRev(String subRev) {
        rev.set(subRev);
    }
}

这是异常详情:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at viewsubject.ViewSubject.read(ViewSubject.java:119)
at viewsubject.ViewSubject.start(ViewSubject.java:113)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more

当代码运行时,然后由于该异常的发生而突然关闭。为什么会发生这种异常?如何删除此异常?

When the code is ran, a window appears for a fraction of a second and then suddenly closes due to the occurence of this exception. Why this exception occurs ? How can remove this exception ?

推荐答案

您没有 MySQL JDBC驱动程序

执行Vogella教程 Java和JDBC与MySQL 。做整个教程一步一步,运行代码每一步的方式。作为Vogella教程的一部分,它会告诉您如何设置第三方库(如MySQL JDBC库)。

Perform the Vogella tutorial on Java and JDBC with MySQL. Do the whole tutorial step by step, running code each step of the way. As part of the Vogella tutorial, it tells you how to setup 3rd party libraries (like the MySQL JDBC library) in Eclipse.

当发布代码时,尝试发布与堆栈跟踪匹配的代码。 StackTrace在viewsubject.ViewSubject.read(ViewSubject.java:119)报告错误,但是在ViewSubject类中没有这样的读取函数。

When posting code, try to post code which matches the stack trace. The StackTrace reports an error at viewsubject.ViewSubject.read(ViewSubject.java:119) but there is no such read function in your ViewSubject class.

这篇关于'在应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException',同时从数据库读取和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆