我正在做一个JavaFX应用程序,其中一部分要求我在.fxml
文件中填充一个表,并从我的数据库中查询ResultSet rs
。
表:h_customers
mysql> desc h_customers;
+-------------------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+-------------+------+-----+---------+----------------+
| intCustomerID | int(11) | NO | PRI | NULL | auto_increment |
| vachCustomerTitle | varchar(25) | NO | | NULL | |
| vachCustomerFirstName | varchar(45) | NO | | NULL | |
| vachCustomerSurnames | varchar(45) | NO | | NULL | |
| dteCustomerDOB | date | NO | | NULL | |
| vachCustomerAddressStreet | varchar(50) | NO | | NULL | |
| vachCustomerAddressTown | varchar(50) | NO | | NULL | |
| vachCustomerAddressCounty | varchar(50) | NO | | NULL | |
| vachCustomerAddressPostalCode | varchar(50) | NO | | NULL | |
| intCustomerHomePhone | int(15) | NO | | NULL | |
| intCustomerWorkPhone | int(15) | NO | | NULL | |
| intCustomerMobilePhone | int(15) | NO | | NULL | |
| vachCustomerEmail | varchar(45) | NO | | NULL | |
+-------------------------------+-------------+------+-----+---------+----------------+
13 rows in set (0.06 sec)
我MainController.java
班的代码段:
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
intCustomerID.setCellValueFactory(new PropertyValueFactory<Customer, String>("intCustomerID"));
vachCustomerTitle.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerTitle"));
vachCustomerFirstName.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerFirstName"));
vachCustomerSurnames.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerSurnames"));
dteCustomerDOB.setCellValueFactory(new PropertyValueFactory<Customer, String>("dteCustomerDOB"));
vachCustomerAddressStreet.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerAddressStreet"));
vachCustomerAddressTown.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerAddressTown"));
vachCustomerAddressCounty.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerAddressCounty"));
vachCustomerAddressPostalCode.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerAddressPostalCode"));
intCustomerHomePhone.setCellValueFactory(new PropertyValueFactory<Customer, String>("intCustomerHomePhone"));
intCustomerWorkPhone.setCellValueFactory(new PropertyValueFactory<Customer, String>("intCustomerWorkPhone"));
intCustomerMobilePhone.setCellValueFactory(new PropertyValueFactory<Customer, String>("intCustomerMobilePhone"));
vachCustomerEmail.setCellValueFactory(new PropertyValueFactory<Customer, String>("vachCustomerEmail"));
tblCustomer.getItems().setAll(parseCustomerList());
}
private Customer parseCustomerList() {
SelectQueries customerRS = new SelectQueries();
customerRS.startcon.connectToDB();
while(customerRS.rs != null){
}
return null;
}
SelectQueries.java
类
public ResultSet getCustomerByID(int intCustomerID) {
startcon.connectToDB();
String sql = null;
sql = "SELECT * "
+ "FROM "
+ "h_customers ";
try {
st.setInt(1, intCustomerID);
rs = st.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
我要填充的.fxml文件中的表:
<TableView fx:id="tblcustomer" prefHeight="171.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn id="intCustomerID" prefWidth="75.0" text="ID" />
<TableColumn id="vachCustomerTitle" prefWidth="75.0" text="Title" />
<TableColumn id="vachCustomerFirstName" prefWidth="75.0" text="First Name" />
<TableColumn id="vachCustomerSurnames" prefWidth="75.0" text="Surname" />
<TableColumn id="dteCustomerDOB" prefWidth="75.0" text="DOB" />
<TableColumn id="vachCustomerAddressStreet" prefWidth="75.0" text="Street Address" />
<TableColumn id="vachCustomerAddressTown" prefWidth="75.0" text="Town" />
<TableColumn id="vachCustomerAddressCounty" prefWidth="75.0" text="County" />
<TableColumn id="vachCustomerAddressPostalCode" prefWidth="75.0" text="Postal Code" />
<TableColumn id="intCustomerHomePhone" prefWidth="75.0" text="Phone (Home)" />
<TableColumn id="intCustomerWorkPhone" prefWidth="75.0" text="Phone (Work)" />
<TableColumn id="intCustomerMobilePhone" prefWidth="75.0" text="Phone (Mobile)" />
<TableColumn id="vachCustomerEmail" prefWidth="75.0" text="Email" />
</columns>
</TableView>