TableView不会返回sqlite数据

时间:2018-05-24 18:29:08

标签: java sql sqlite javafx

无法弄清楚数据未显示的原因。

MainLayout嵌入在TabLayout

  

MainLayout.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ComboBox fx:id="repartoCompo" layoutX="25.0" layoutY="58.0" prefWidth="150.0" />
      <Label layoutX="25.0" layoutY="33.0" text="Seleziona Reparto" />
      <ComboBox fx:id="attivitaCombo" layoutX="25.0" layoutY="164.0" prefWidth="150.0" />
      <Label layoutX="25.0" layoutY="139.0" text="Seleziona Attività" />
      <TableView fx:id="tableMain" layoutX="200.0" layoutY="17.0" prefHeight="671.0" prefWidth="1152.0">
        <columns>
          <TableColumn fx:id="nomeCol" prefWidth="75.0" text="Nome" />
          <TableColumn fx:id="repartoCol" prefWidth="75.0" text="Reparto" />
          <TableColumn fx:id="attivitaCol" prefWidth="75.0" text="Attività" />
          <TableColumn fx:id="giornoCol" prefWidth="75.0" text="Giorno" />
          <TableColumn fx:id="finegiornoCol" prefWidth="75.0" text="Fine giorno" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
  

TabLayout提取以显示布局之间的链接

 <TabPane fx:id="tabPane" layoutY="6.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" tabClosingPolicy="UNAVAILABLE" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
        <tabs>
          <Tab text="Visualizza">
            <fx:include source="MainLayout.fxml" />
          </Tab>
  

Operatori.java(模型)

package application;

import javafx.beans.property.SimpleStringProperty;

public class Operatori {


        private  SimpleStringProperty nome = new SimpleStringProperty();
        private  SimpleStringProperty reparto = new SimpleStringProperty();
        private  SimpleStringProperty attivita = new SimpleStringProperty();
        private  SimpleStringProperty giorno = new SimpleStringProperty();
        private  SimpleStringProperty finegiorno = new SimpleStringProperty();

        public String getNome() {
            return nome.get();
        }

        public void setNome(String nomeIn) {
            nome.set(nomeIn);
        }

        public String getReparto() {
            return reparto.get();
        }

        public void setReparto(String repartoIn) {
            reparto.set(repartoIn);
        }

        public String getAttivita() {
            return attivita.get();
        }

        public void setAttivita(String attivitaIn) {
            attivita.set(attivitaIn);
        }

        public String getGiorno() {
            return giorno.get();
        }

        public void setGiorno(String giornoIn) {
            giorno.set(giornoIn);
        }

        public String getFinegiorno() {
            return finegiorno.get();
        }

        public void setFineGiorno(String finegiornoIn) {
            finegiorno.set(finegiornoIn);
        }

    }
  

主要课程

package application;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;



public class Main extends Application {

    Connection conn;
    PreparedStatement pst = null;
    ResultSet rs = null;


    @Override
    public void start(Stage primaryStage) {
        try {
            CheckConnection();
            Parent root = FXMLLoader.load(getClass().getResource("TabLayout.fxml"));
            Scene scene = new Scene(root,600,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setTitle("Gestione Personale");
            primaryStage.setScene(scene);
            primaryStage.show();
            primaryStage.setMaximized(true);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void CheckConnection(){
        conn = SqlConnection.DbConnector();
        if(conn == null){
            System.out.println("Connection not successful");
            System.exit(1);
        }
        else{
            System.out.println("Connection  successful");
        }
    }

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

OperatoriController

package application;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class OperatoriController {

    @FXML
    private TableView<Operatori> tableMain;

    @FXML
    private TableColumn<Operatori,String> nomeCol;

    @FXML
    private TableColumn<Operatori,String> repartoCol;

    @FXML
    private TableColumn<Operatori,String> attivitaCol;

    @FXML
    private TableColumn<Operatori,String> giornoCol;
    @FXML
    private TableColumn<Operatori,String> finegiornoCol;

    //START | SQLITE
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    //END | SQLITE

    private ObservableList <Operatori> listaMain; 

    @FXML
    public void initialize() {
        listaMain = FXCollections.observableArrayList();

        nomeCol.setCellValueFactory(
                new PropertyValueFactory<Operatori, String>("nome")
        );
        repartoCol.setCellValueFactory(
                new PropertyValueFactory<Operatori, String>("reparto")
        );
        attivitaCol.setCellValueFactory(
                new PropertyValueFactory<Operatori, String>("attivita")
        );
        giornoCol.setCellValueFactory(
                new PropertyValueFactory<Operatori, String>("giorno")
        );
        finegiornoCol.setCellValueFactory(
                new PropertyValueFactory<Operatori, String>("finegiorno")
        );


        try {            
            con = SqlConnection.DbConnector();
            stat = con.createStatement();
            ResultSet rs = con.createStatement().executeQuery("SELECT NOME,Reparto,Attivita,Day,FineDay FROM Visualizza");
            System.out.println(rs);
            while (rs.next()) {
                Operatori nt = new Operatori();
                nt.setNome(rs.getString("NOME"));
                nt.setReparto(rs.getString("Reparto"));
                nt.setAttivita(rs.getString("Attivita"));
                nt.setGiorno(rs.getString("Day"));
                nt.setFineGiorno(rs.getString("FineDay"));
                listaMain.add(nt);
            }            
            tableMain.setItems(listaMain);


        } catch (SQLException ex) {
            Logger.getLogger(OperatoriController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
  

连接到DB类

package application;
import java.sql.*;

public class SqlConnection 
{
    public static Connection DbConnector()
    {
    Connection conn=null;
        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:C:/Users/Utente/Desktop/DB/prova.db");

            return conn;

            }catch(ClassNotFoundException | SQLException e)
             {
              System.out.println(e);

             } 
        return null;
    }

}

我试图到处寻找,但我没有得到它,与db的连接说&#34;成功&#34;。 列仍为空。 我不认为这是一个命名问题因为我多次检查过但是我可能错了。

1 个答案:

答案 0 :(得分:1)

正如James_D指出的那样,你的控制器类永远不会被使用。您需要在FXML中指定控制器:

fx:controller="application.OperatoriController.java"

或者从Main.java加载FXML时指定控制器:

try {
    CheckConnection();

    FXMLLoader loader = new FXMLLoader();
    loader.setController(new OperatoriController());

    Parent root = loader.load(getClass().getResource("TabLayout.fxml"));
    Scene scene = new Scene(root,600,400);
    scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
    primaryStage.setTitle("Gestione Personale");
    primaryStage.setScene(scene);
    primaryStage.show();
    primaryStage.setMaximized(true);
} catch(Exception e) {
    e.printStackTrace();
}