我正在使用JavaFX,但我的组合框不想从SQL数据库填充



我要做的是在加载应用程序时,用travelxeperts数据库中的AgentID填充我的组合框。

我认为问题出在控制器类中。我没有得到任何异常,但如果我尝试从其中一个按钮运行buildCombo((方法而不是初始化,我会得到一个未找到类的异常

控制器类别:

package sample;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
public class Controller {
Connection conn;
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextField tvAgentID;
@FXML
private TextField tvFirstName;
@FXML
private TextField tvMiddleInitial;
@FXML
private TextField tvLastName;
@FXML
private TextField tvPhone;
@FXML
private TextField tvEmail;
@FXML
private TextField tvPosition;
@FXML
private TextField tvAgencyID;
@FXML
private Button btnSave;
@FXML
private Button btnEdit;
@FXML
private ComboBox cmbAgentID;
public Controller() throws SQLException {
}
@FXML
void cmbClicked(MouseEvent event) {
}
@FXML
void editClicked(MouseEvent event) {
}
@FXML
void saveClicked(MouseEvent event) throws SQLException, ClassNotFoundException {
updateAgent();
}

/////////////////////////////METHODS/////////////////////

private void buildCombo() throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
Connection conn;
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/travelexperts",
"Sander", "********");
ObservableList<Agent> data = FXCollections.observableArrayList();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select AgentId from agents");
while (rs.next()) {
data.add(new Agent(rs.getInt("agentID")));
}
cmbAgentID.setItems(data);
}
///////////////////INITIALIZE/////////////////////////////
@FXML
public void initialize(URL location, ResourceBundle resources) throws SQLException, ClassNotFoundException {
assert tvAgentID != null : "fx:id="tvAgentID" was not injected: check your FXML file 'sample.fxml'.";
assert tvFirstName != null : "fx:id="tvFirstName" was not injected: check your FXML file 'sample.fxml'.";
assert tvMiddleInitial != null : "fx:id="tvMiddleInitial" was not injected: check your FXML file 'sample.fxml'.";
assert tvLastName != null : "fx:id="tvLastName" was not injected: check your FXML file 'sample.fxml'.";
assert tvPhone != null : "fx:id="tvPhone" was not injected: check your FXML file 'sample.fxml'.";
assert tvEmail != null : "fx:id="tvEmail" was not injected: check your FXML file 'sample.fxml'.";
assert tvPosition != null : "fx:id="tvPosition" was not injected: check your FXML file 'sample.fxml'.";
assert tvAgencyID != null : "fx:id="tvAgencyID" was not injected: check your FXML file 'sample.fxml'.";
assert btnSave != null : "fx:id="btnSave" was not injected: check your FXML file 'sample.fxml'.";
assert btnEdit != null : "fx:id="btnEdit" was not injected: check your FXML file 'sample.fxml'.";
assert cmbAgentID != null : "fx:id="cmbAgentID" was not injected: check your FXML file 'sample.fxml'.";
btnSave.setStyle("-fx-background-color: cornflowerblue");
btnEdit.setStyle("-fx-background-color: cornflowerblue");
try {
conn = new DB().getConnection();
buildCombo();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

DB类:

package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB {
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/travelexperts",
"Sander",
"*******"
);
}
}

主要类别:

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}

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

样品类别:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TextField fx:id="tvAgentID" layoutX="14.0" layoutY="52.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvFirstName" layoutX="143.0" layoutY="52.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvMiddleInitial" layoutX="290.0" layoutY="49.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvLastName" layoutX="447.0" layoutY="52.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvPhone" layoutX="24.0" layoutY="321.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvEmail" layoutX="161.0" layoutY="321.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvPosition" layoutX="317.0" layoutY="321.0" prefHeight="26.0" prefWidth="89.0" />
<TextField fx:id="tvAgencyID" layoutX="475.0" layoutY="321.0" prefHeight="26.0" prefWidth="89.0" />
<Label alignment="CENTER" layoutX="18.0" layoutY="11.0" prefHeight="26.0" prefWidth="81.0" text="AgentID" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="147.0" layoutY="14.0" prefHeight="26.0" prefWidth="81.0" text="First Name" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="294.0" layoutY="14.0" prefHeight="26.0" prefWidth="90.0" text="Middle Initial" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="451.0" layoutY="14.0" prefHeight="26.0" prefWidth="81.0" text="Last Name" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="28.0" layoutY="298.0" prefHeight="26.0" prefWidth="81.0" text="Phone" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="165.0" layoutY="298.0" prefHeight="26.0" prefWidth="81.0" text="Email" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="321.0" layoutY="298.0" prefHeight="26.0" prefWidth="81.0" text="Position" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="479.0" layoutY="298.0" prefHeight="26.0" prefWidth="81.0" text="AgencyID" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="123.0" prefHeight="62.0" prefWidth="235.0" text="EDIT AN AGENT">
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<Button fx:id="btnSave" layoutX="447.0" layoutY="126.0" mnemonicParsing="false" onMouseClicked="#saveClicked" prefHeight="56.0" prefWidth="120.0" text="Save" />
<Button fx:id="btnEdit" layoutX="447.0" layoutY="200.0" mnemonicParsing="false" onMouseClicked="#editClicked" prefHeight="56.0" prefWidth="120.0" text="Edit" />
<ComboBox fx:id="cmbAgentID" layoutX="235.0" layoutY="140.0" onMouseClicked="#cmbClicked" prefHeight="28.0" prefWidth="130.0" />
</children>
</AnchorPane>

代理人类别:

package sample;
public class Agent {
private int agentID;
private String firstName;
private String middleInitial;
private String lastName;
private String businessPhone;
private String email;
private String position;
private int agencyID;
//////////////Constructors////////////////
public Agent(int agentID, String firstName, String middleInitial, String lastName,
String businessPhone, String email, String position, int agencyID) {
this.agentID = agentID;
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.businessPhone = businessPhone;
this.email = email;
this.position = position;
this.agencyID = agencyID;
}
public Agent(int agentID) {
this.agentID = agentID;
}

DB Class中,您使用的是正确的MySQL驱动程序,但在buildCombo()方法中,您为MySQL使用了错误/不推荐使用的驱动程序,请将Class.forName("com.mysql.cj.jdbc.Driver");更改为Class.forName("com.mysql.jdbc.Driver");,就像在DB Class中一样,它应该可以工作。此外,在控制台中打印出ResultSet rs,这样您就可以确信您在那里得到了一些结果。

相关内容

  • 没有找到相关文章

最新更新