Java运行出现except,'在应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException',同时从数据库读取和显示...

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 table = new TableView<>();

private final ObservableList 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("subName"));

sub.setCellFactory(TextFieldTableCell.forTableColumn());

sub.setOnEditCommit(

new EventHandler>() {

@Override

public void handle(TableColumn.CellEditEvent 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("subCode"));

code.setCellFactory(TextFieldTableCell.forTableColumn());

code.setCellFactory(TextFieldTableCell.forTableColumn());

code.setOnEditCommit(

new EventHandler>() {

@Override

public void handle(TableColumn.CellEditEvent t) {

((Subject) t.getTableView().getItems().get(

t.getTablePosition().getRow())).setSubCode(t.getNewValue());

}

}

);

TableColumn rev = new TableColumn("Revision");

rev.setMinWidth(130);

rev.setCellValueFactory(

new PropertyValueFactory("subRev"));

rev.setCellFactory(TextFieldTableCell.forTableColumn());

rev.setCellFactory(TextFieldTableCell.forTableColumn());

rev.setOnEditCommit(

new EventHandler>() {

@Override

public void handle(TableColumn.CellEditEvent 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);

}

}

This is the exception details :

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 ?

解决方案

You don't have the MySQL JDBC driver on your classpath.

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值