在JavaFX TableView中显示Oracle数据库内容

时间:2014-10-21 14:16:40

标签: database oracle javafx tableview

我想在TableView“FilmTable”中显示Oracle数据库的内容。 Oracle Databade的结构是:表“LUKA1”中名为“FILMTITEL”的表列。有4个电影名称,如速度与激情,......

希望你能帮助我,我希望我理解你的解决方案来解决我的问题。

我制作了这段代码,我得到了这个错误:

    javafx.fxml.LoadException: 
/D:/Users/muellerl/workspace/Table_DB/bin/application/gui.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at controller.table_controller.initialize(table_controller.java:39)
    ... 20 more

如果删除所有Table-Code-Contents,则不存在错误。

这是我的代码,希望你能帮我展示这些东西。

Main.java

    package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource(
                    "/application/gui.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

table_controller.java:

package controller;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import oracle.jdbc.*;
import model.filmtable_data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class table_controller implements Initializable {
    Connection conn;
    String output;
    int zaehler = 0;

    @FXML
    private TableView<filmtable_data> FilmTable;
    @FXML
    private TableColumn<filmtable_data, String> Filmtitel_col;
    @FXML
    private TableColumn<filmtable_data, Integer> ID_col;



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // Observable List
        final ObservableList<filmtable_data> data = FXCollections.observableArrayList();
        ID_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, Integer>("rID"));
        Filmtitel_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, String>("rFilmtitel"));
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:lukas/1234@10.140.79.39:1521:OTTO");
            Statement statement = conn.createStatement();
            ResultSet resultset = statement.executeQuery("SELECT FILMTITEL FROM LUKA1");

            while (resultset.next()) {
                output = resultset.getString(1);
                zaehler++;
                filmtable_data entry = new filmtable_data(zaehler, output);
                data.add(entry);
                System.out.println (resultset.getString(1));
                System.out.println(output);
                }
            FilmTable.setItems(data);
            statement.close();
        }   catch (SQLException e) {
                System.out.println("Login fehlgeschlagen.");
                e.printStackTrace();
        }
    }   
}

filmtable_data.java:

package model;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class filmtable_data {

    private final SimpleIntegerProperty rID;
    private final SimpleStringProperty rFilmtitel;

    public filmtable_data (Integer sID, String sFilmtitel) {
        this.rID = new SimpleIntegerProperty(sID);
        this.rFilmtitel = new SimpleStringProperty(sFilmtitel);
    }

    public Integer getRID() {
        return rID.get();
    }

    public void setRID(int set) {
        rID.set(set);
    }

    public String getRFilmtitel() {
        return rFilmtitel.get();
    }

    public void setRFilmtitel(String set) {
        rFilmtitel.set(set);
    }
}

0 个答案:

没有答案