What I’m trying to do is have my combobox get populated with AgentID’s from my travelexperts database when the app loads.
I think the problem is somewhere in the controller class. I don’t get any exceptions but if I try to run the buildCombo() method from one of the buttons instead of initialize I get a class not found exception
Controller class:
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 class:
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", "*******" ); } }
Main Class:
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); } }
Sample class:
<?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>
Agent Class:
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; }
Advertisement
Answer
In your DB Class
you are using the right MySQL Driver but in your buildCombo()
Method you are using wrong/deprecated driver for MySQL, change Class.forName("com.mysql.cj.jdbc.Driver");
to Class.forName("com.mysql.jdbc.Driver");
as you have in DB Class
and it should work. Also, print out the ResultSet rs
in the console, so you are sure that you are having some result there.