gpt4 book ai didi

java - 我正在使用 JavaFX,并且我的组合框不想从 sql 数据库填充

转载 作者:行者123 更新时间:2023-12-02 08:49:01 25 4
gpt4 key购买 nike

我想做的是在应用程序加载时使用我的 Travelexperts 数据库中的 AgentID 填充我的组合框。

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

Controller 类:

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();
}
}

数据库类:

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,这样您就可以确定在那里得到了一些结果。

关于java - 我正在使用 JavaFX,并且我的组合框不想从 sql 数据库填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60906289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com