0
所以我能夠得到sql查詢來運行和更新我的數據庫中的數據,但我無法讓我的文本域更新與計算。我認爲這將會像選擇新的數據庫數量並將其注入文本框一樣簡單,但我有一種感覺,我沒有做到這一點。我可以在我的應用程序中加載數據庫表,然後得到新的結果,但我更喜歡它自動顯示。JavaFX textfield不更新與MYSQL查詢
下面
@FXML
private TextField orderBox;
@FXML
private TextField totalBox;
public void calculateOrder(ActionEvent event){
String total = totalBox.getText();
String product = productBox.getText();
String queryUpdate="UPDATE ordertable o JOIN product p ON p.productID = o.productID set o.total = o.amount * p.cost";
String updateBox="SELECT total FROM ordertable WHERE productID = ?";
try{
query=c.prepareStatement(queryUpdate);
update=c.prepareStatement(updateBox);
update.setString(1, total);
query.execute();
update.execute();
//usetString(2, productID);
query.close();
update.close();
rs.close();
Alert confirmation = new Alert(Alert.AlertType.CONFIRMATION, "Calculated");
confirmation.show();
}
catch(SQLException e){
System.out.println(e);
}
的FXML類控制器的代碼。
<AnchorPane prefHeight="420.0" prefWidth="627.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.OrderController">
<children>
<AnchorPane layoutX="14.0" layoutY="14.0" prefHeight="331.0" prefWidth="419.0">
<children>
<Label layoutX="164.0" layoutY="38.0" text="Order" />
<Label layoutX="14.0" layoutY="86.0" text="orderID" />
<Label layoutX="13.0" layoutY="128.0" text="productID" />
<Label layoutX="3.0" layoutY="167.0" text="CustomerID" />
<Label layoutX="28.0" layoutY="233.0" text="Date" />
<TextField id="orderIDBox" fx:id="orderBox" layoutX="67.0" layoutY="82.0" prefHeight="25.0" prefWidth="90.0" promptText="Enter OrderID" />
<TextField fx:id="productBox" layoutX="67.0" layoutY="124.0" prefHeight="25.0" prefWidth="90.0" />
<TextField fx:id="customerBox" layoutX="67.0" layoutY="163.0" prefHeight="25.0" prefWidth="90.0" />
<TextField fx:id="dateBox" layoutX="67.0" layoutY="229.0" prefHeight="25.0" prefWidth="90.0" />
<TextField id="totalBox" fx:id="totalBox" layoutX="89.0" layoutY="330.0" prefHeight="25.0" prefWidth="90.0" />
<TableView fx:id="orderUser" layoutX="164.0" layoutY="74.0" onMouseClicked="#showOnClick" prefHeight="248.0" prefWidth="447.0">
<columns>
<TableColumn fx:id="columnOrder" prefWidth="75.0" text="orderID" />
<TableColumn fx:id="columnProduct" prefWidth="75.0" text="productID" />
<TableColumn fx:id="columnCustomer" prefWidth="75.0" text="customerID" />
<TableColumn fx:id="columnDate" prefWidth="75.0" text="Date" />
<TableColumn fx:id="columnAmount" prefWidth="62.0" text="Amount" />
<TableColumn fx:id="columnTotal" prefWidth="75.0" text="total" />
</columns>
</TableView>
<Button id="loadButton" layoutX="518.0" layoutY="43.0" mnemonicParsing="false" onAction="#loadDataFromDatabase" text="Load" />
<Button id="editButton" fx:id="editButton" layoutX="94.0" layoutY="293.0" mnemonicParsing="false" onAction="#updateOrder" text="Edit" />
<Button id="deleteButton" fx:id="deleteButton" layoutX="15.0" layoutY="293.0" mnemonicParsing="false" onAction="#deleteOrder" text="Delete" />
<Button id="calculateButton" fx:id="calculateButton" layoutX="20.0" layoutY="330.0" mnemonicParsing="false" onAction="#calculateOrder" text="Calculate" />
<Label layoutX="19.0" layoutY="200.0" text="Amount" />
<TextField fx:id="amountBox" layoutX="67.0" layoutY="196.0" prefHeight="25.0" prefWidth="90.0" />
</children>
</AnchorPane>
<Button fx:id="addButton" layoutX="220.0" layoutY="363.0" mnemonicParsing="false" onAction="#AddOrder" text="Add" />
</children>
</AnchorPane>
對不起,但是這是總計,到目前爲止我沒有運氣試圖分配它。 –
添加更多的信息,所以也許我可以幫助你。 – Clams
如果你這樣做,你應該可以得到這些信息: int total = 0; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(updateBox); (rs.next()){ total = rs.getint(「total」); } – Clams