如何用SQL数据填充TableView

时间:2016-01-07 14:52:34

标签: sql javafx scenebuilder

我一直在尝试使用从数据库中查询的数据加载TableView,但似乎无法让它工作。

这是我尝试使用数据库查询项填充数据库的第一次尝试,以防我的代码看起来很糟糕且远非好用。

FXML是通过JavaFx SceneBuilder完成的。

这是数据库查询类:

public static void refreshTableApplicant(){
      dataApplicant = FXCollections.observableArrayList();
      try {
            Connection con = Login.getConnection();
            Statement stmt = con.createStatement();
            if (Login.getEntitlement() < 3) {
                ResultSet rs = stmt.executeQuery("Select * from applicant");
                while (rs.next()) {
                    ObservableList<String> applicant = FXCollections.observableArrayList();
                    applicant.add(rs.getString(1));
                    applicant.add(rs.getString(5));
                    applicant.add(rs.getString(6));
                    applicant.add(rs.getString(8));
                    applicant.add(rs.getString(9));
                    applicant.add(rs.getString(10));
                    applicant.add(rs.getString(11));
                    applicant.add(rs.getString(13));
                    applicant.add(rs.getString(14));
                    applicant.add(rs.getString(16));
                    applicant.add(rs.getString(19));
                    applicant.add(rs.getString(7));
                    applicant.add(rs.getString(20));
                    dataApplicant.add(applicant);
                    System.out.println(dataApplicant);

                }
            }
            else {
                ResultSet rs = stmt.executeQuery(
                        "Select * from applicant a inner join relationship r on r.applicantID = a.applicantID inner join vacancy v on v.vacancyID = r.vacancyID where v.divisionID = "
                                + Login.getDivisionID());
                while (rs.next()) {
                    ObservableList<String> applicant = FXCollections.observableArrayList();
                    applicant.add(rs.getString(1));
                    applicant.add(rs.getString(5));
                    applicant.add(rs.getString(6));
                    applicant.add(rs.getString(8));
                    applicant.add(rs.getString(9));
                    applicant.add(rs.getString(10));
                    applicant.add(rs.getString(11));
                    applicant.add(rs.getString(13));
                    applicant.add(rs.getString(14));
                    applicant.add(rs.getString(16));
                    applicant.add(rs.getString(19));
                    applicant.add(rs.getString(7));
                    applicant.add(rs.getString(20));                           
                    dataApplicant.add(applicant);
                }
            }
             tableViewApplicant.getItems().addAll(dataApplicant);
        } catch (SQLException e) {
            System.out.println("MainWindowController - refreshTableApplicant");
            e.printStackTrace();
        }         
}   

这是我得到的错误:

[[100002, Doe, John, Mainstreet, 2, 14572, Chicago, 045404245, 156451231, Doe.John@googlemail.com, Manager, 1994-11-24, MIT]]
java.lang.NullPointerException
at trilabfx.GUI.MainWindowController.refreshTableApplicant(MainWindowController.java:114)
at trilabfx.login.Login.login(Login.java:30)
at trilabfx.login.LoginController.btnLoginPressed(LoginController.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$197/522491302.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

这是FXML文件的一部分:

<Tab fx:id="tabApplicant" text="applicant">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="1080.0" prefWidth="1920.0">
                       <children>
                          <HBox layoutX="234.0" layoutY="116.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                             <children>
                                <TableView fx:id="tableViewApplicant" layoutX="234.0" layoutY="116.0" prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
                                  <columns>
                                    <TableColumn prefWidth="-1.0" text="applicant-ID" />
                                    <TableColumn prefWidth="-1.0" text="name" />
                                    <TableColumn prefWidth="-1.0" text="firstName" />
                                    <TableColumn prefWidth="-1.0" text="street" />
                                    <TableColumn prefWidth="-1.0" text="houseNr" />
                                    <TableColumn prefWidth="-1.0" text="postalCode" />
                                    <TableColumn prefWidth="-1.0" text="city" />
                                    <TableColumn prefWidth="-1.0" text="telefon home" />
                                    <TableColumn prefWidth="-1.0" text="telefon mobil" />
                                    <TableColumn prefWidth="-1.0" text="e-mail" />
                                    <TableColumn prefWidth="-1.0" text="vacancy" />
                                    <TableColumn prefWidth="-1.0" text="birthday" />
                                    <TableColumn prefWidth="-1.0" text="educationalAchievement" />
                                  </columns>
                                   <columnResizePolicy>
                                      <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                                   </columnResizePolicy>
                                </TableView>
                             </children>
                          </HBox>
                       </children></AnchorPane>
              </content>
            </Tab>

该方法由Login类访问:

    public static int login(String username, String password) {
    try {
        con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=trilab;user=trilab;password=trilab");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT entitlement, employeeID, divisionID from employee where loginName = '"+ username + "' AND password = '" + password + "'");
        while (rs.next()) {
            entitlement = rs.getInt(1);
            ID = rs.getInt(2);
            divisionID = rs.getInt(3);
            MainWindowController.refreshTableApplicant();
            return entitlement;
        }
    } catch (Exception e) {
        System.out.println("Login - login");
        e.printStackTrace();
    }
    return 0;
}

0 个答案:

没有答案