如何使用javafx和数据库源代码(在本例中为SQLite)在combobox上应用MVC模式



我正在尝试将以前的javafx程序转换为MVC机器。我实际上没有任何编译错误,也没有运行时错误。但是当我运行登录应用程序时,我无法在组合框中看到数据库值。

是的,我当然可以登录,它很好用。

这是我为分为LoginView、LoginController和LoginModel:的组合框编写的代码

package com.login;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ComboBox;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * Created by DELL PC on 7/25/2016.
 */
public class LoginModel {
    Connection connection;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    public LoginModel()
    {
        connection = SqliteConnection.connector();
        if(connection == null)
       {
        System.exit(1);
    }
}
    public boolean isDBConnected()
   {
    try {
        return !connection.isClosed();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }
}
public ComboBox fillCombobox()
{
    try
    {
        final ObservableList options = FXCollections.observableArrayList();
        String query = "SELECT role from admin";
        preparedStatement = connection.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next())
        {
            options.add(resultSet.getString("role"));
        }
        preparedStatement.close();
        resultSet.close();
    }
    catch(SQLException ex)
    {
        Logger.getLogger(LoginModel.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
public boolean isLogin(String user, String pass) throws SQLException
{
    String query = "SELECT * FROM admin WHERE username = ? and password = ?";
    try {
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, user);
        preparedStatement.setString(2, pass);
        resultSet = preparedStatement.executeQuery();
        if(resultSet.next())
        {
            return true;
        }
        else
        {
            return false;
        }
    }catch(Exception e){
        return false;
    }
    finally
    {
        preparedStatement.close();
        resultSet.close();
    }
}

}

控制器代码

 package com.login;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class LoginController implements Initializable{
    public LoginModel loginModel = new LoginModel();
    @FXML
    private TextField txtusername;
    @FXML
    private PasswordField pwpaswword;
    @FXML
    private Label isConnected;
    @FXML
    private ComboBox comboBox;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        comboBox.getItems().contains(loginModel.fillCombobox());
    if(loginModel.isDBConnected())
    {
        isConnected.setText("Connected");
    }
    else
    {
        isConnected.setText("Not Connected");
    }
}
public void Login(ActionEvent event)
{
    try {
            if (loginModel.isLogin(txtusername.getText(), pwpaswword.getText()))
            {
                isConnected.setText("Login Successfully ");
                ((Node)event.getSource()).getScene().getWindow().hide();
                Stage window = new Stage();
                FXMLLoader loader = new FXMLLoader();
                Pane root = loader.load(getClass().getResource("CivilStateView.fxml").openStream());
                CivilStateController civilStateController = (CivilStateController)loader.getController();
                civilStateController.getUser(txtusername.getText());
                Scene scene = new Scene(root);
                window.setTitle("Hello World");
                window.setScene(scene);
                window.show();
            }
            else
            {
                isConnected.setText("Login Unsuccessful");
            }
        } catch (SQLException e) {
            isConnected.setText("Exception occurred:" + e);
            e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

视图是用fxml:制作的

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="346.0" prefWidth="360.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.login.LoginController">
   <children>
      <Label fx:id="isConnected" layoutX="31.0" layoutY="30.0" prefHeight="36.0" prefWidth="150.0" text="Status" textFill="#f20f0f">
     <font>
        <Font size="18.0" />
     </font>
  </Label>
  <TextField fx:id="txtusername" layoutX="26.0" layoutY="75.0" prefHeight="48.0" prefWidth="160.0" promptText="Username">
     <font>
        <Font size="19.0" />
     </font>
  </TextField>
  <PasswordField fx:id="pwpaswword" layoutX="26.0" layoutY="137.0" prefHeight="48.0" prefWidth="160.0" promptText="Password">
     <font>
        <Font size="19.0" />
     </font>
  </PasswordField>
  <ComboBox fx:id="comboBox" layoutX="26.0" layoutY="196.0" prefHeight="36.0" prefWidth="150.0">
          <padding>
             <Insets left="20.0" />
          </padding>
      </ComboBox>
      <Button layoutX="26.0" layoutY="245.0" mnemonicParsing="false" onAction="#Login" prefHeight="36.0" prefWidth="68.0" text="Button" />
   </children>
</AnchorPane>

我能够创建一个测试用例来运行它,尽管不使用任何DB交互。关键部分包括:

public class LoginModel {
    ...
    public ObservableList fillCombobox() {
        ObservableList options = FXCollections.observableArrayList();
        // replace this with your DB code to add the options.
        for (int i = 1; i < 10; i++) {
            options.add("Role " + i);
        }
        //
        return options;
    }
}
public class LoginController implements Initializable {
    ...
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        comboBox.setItems(loginModel.fillCombobox());
        ...
    }
}

最新更新