首页 > 解决方案 > 运行 javafx 数据库项目时出错

问题描述

我试图使用场景构建器在 JavaFX 中构建数据库项目。设计、创建 CSS 文件和安装 derby 数据库库时一切正常。但是当我运行程序时,我得到了错误:

> Exception in Application start method
> java.lang.reflect.InvocationTargetException   at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:498)     at
> com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
>   at
> com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:498)     at
> sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
> Caused by: java.lang.RuntimeException: Exception in Application start
> method    at
> com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
>   at
> com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
>   at java.lang.Thread.run(Thread.java:748) Caused by:
> java.lang.UnsupportedClassVersionError:
> org/apache/derby/jdbc/EmbeddedDriver has been compiled by a more
> recent version of the Java Runtime (class file version 53.0), this
> version of the Java Runtime only recognizes class file versions up to
> 52.0  at java.lang.ClassLoader.defineClass1(Native Method)    at java.lang.ClassLoader.defineClass(ClassLoader.java:756)  at
> java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
>   at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)     at
> java.net.URLClassLoader.access$100(URLClassLoader.java:74)    at
> java.net.URLClassLoader$1.run(URLClassLoader.java:369)    at
> java.net.URLClassLoader$1.run(URLClassLoader.java:363)    at
> java.security.AccessController.doPrivileged(Native Method)    at
> java.net.URLClassLoader.findClass(URLClassLoader.java:362)    at
> java.lang.ClassLoader.loadClass(ClassLoader.java:418)     at
> sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)     at
> java.lang.ClassLoader.loadClass(ClassLoader.java:351)     at
> java.lang.Class.forName0(Native Method)   at
> java.lang.Class.forName(Class.java:264)   at
> db.db.Createconnection(db.java:34)    at db.db.<init>(db.java:27)     at
> javafxapplication3.FXMLDocumentController.initialize(FXMLDocumentController.java:36)
>   at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)    at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)     at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)     at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)     at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)     at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)     at
> javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)     at
> javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)     at
> javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
>   at
> com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
>   at
> com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
>   at
> com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
>   at java.security.AccessController.doPrivileged(Native Method)   at
> com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
>   ... 1 more Exception running application
> javafxapplication3.JavaFXApplication3 Java Result: 1

通过搜索类似的问题,我发现我可能缺少一个叫做 VM 选项的东西(虽然我不太确定,那是什么)

所以我在 netbeans 项目的 VM 选项中添加了这一行:

--module-path C:\javafx-sdk-11.0.2\lib --add-modules javafx.controls,javafx.fxml

这次我遇到了一个新错误:

**

错误:无法创建 Java 虚拟机。错误:发生了致命异常。程序将退出。无法识别的选项:--module-path Java 结果:1

现在我不知道如何摆脱它。

我的简单项目中共有 4 个文件。主要功能在这里:

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

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

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

这是 XML 文档控制器:

@FXML
private Label label;
@FXML
private Button button;

private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
     db dbh = new db();
}    

@FXML
private void pri(ActionEvent event) {
    System.out.print("hi");
}

这是创建与数据库的连接的代码:

public db()
{
    Createconnection();
    setupbooktable();
}
void Createconnection()
{
    try
    {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        conn=DriverManager.getConnection(db_url);

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

void setupbooktable()
{

    String tablename="Book";
    try
    {
        stam=conn.createStatement();
        DatabaseMetaData dmd=conn.getMetaData();
        ResultSet tables=dmd.getTables(null, null, tablename.toUpperCase(), null);
        if(tables.next())
        {
            System.out.println("Table"+tablename+"exists already!Ready to go!!");

        }
        else
        {
            stam.execute("CREATE TABLE"+tablename+"("
                    + "id varchar(200) primary key ,\n"
                    + "title varchar(200),\n"
                    + "author varchar(200),\n"
                    + "publisher varchar(200),\n"
                    + "intcode varchar(200),\n"
                    + "isavailable boolean default true"
                    +")");



        }




    }
    catch(SQLException e)
    {
        System.err.printf(e.getMessage()+"error in database...");

    }
    finally
    {

    }

}


I have no issues in importing packages. This project just prints a message on the prompt when the button is pressed. And try to create a database table.

谢谢。

标签: javadatabasejavafxnetbeansvirtual-machine

解决方案


推荐阅读