JavaFX文本字段未使用MYSQL查询更新
因此,我可以让sql查询运行并更新数据库中的数据,但无法让textfield随计算更新。我想这就像选择新的数据库数量并将其注入文本字段一样简单,但我有一种感觉,我没有正确地做到这一点。我可以在我的应用程序中加载数据库表,然后得到新的结果,但我更希望它自动显示出来 下面是控制器代码JavaFX文本字段未使用MYSQL查询更新,mysql,javafx,Mysql,Javafx,因此,我可以让sql查询运行并更新数据库中的数据,但无法让textfield随计算更新。我想这就像选择新的数据库数量并将其注入文本字段一样简单,但我有一种感觉,我没有正确地做到这一点。我可以在我的应用程序中加载数据库表,然后得到新的结果,但我更希望它自动显示出来 下面是控制器代码 @FXML private TextField orderBox; @FXML private TextField totalBox; public void calculateOrder(ActionEvent e
@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>
据我所知(因为我看不到所有代码),您只是在执行查询。您没有将结果赋值为nothing。
您的更新和查询都可以正常工作,但由于您没有将其分配给任何对象,因此您将看不到更改
尝试将查询结果分配给您的文本字段(我不知道哪个是文本字段,因为您没有说要更新哪一个,我猜是总数)。然后您将能够在您的文本字段中看到更新很抱歉,但它是总数,到目前为止,我还没有幸运地尝试分配它。如果您这样做,您应该能够获得信息:int total=0;语句stmt=conn.createStatement();结果集rs=stmt.executeQuery(updateBox);而(rs.next(){total=rs.getint(“total”);}那么……你做了什么?