为什么我的数据库会恢复旧信息?

时间:2015-02-02 15:20:42

标签: java sql database netbeans h2

我在我的软件应用程序中使用H2嵌入式数据库。当提取销售对话框时,它允许用户输入客户名称,然后开始插入项目编号。当项目编号被添加并按下“输入”键时,手头的数量(在数据库中)减少1.如果从表中删除该项目,则将数量添加回数据库。这是为了在用户用完特定项目时为用户提供实时更新,并让他们知道如果他们确实手头有这个项目,他们需要确保在库存中更新它编辑器。

此外,如果销售被取消,所有商品都会被添加回库存。

我尝试同时运行我的应用程序的两个实例,一个用于开始输入销售信息,另一个用于检查库存编辑器中的项目数量。它应该保持实时更新。但是,当我点击“完成销售”时,数量将恢复为旧数量。

以下是我的CompleteSaleButton的代码:

private void CompleteSaleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    String sChangeDue = ChangeDueTextField.getText();
    double dChangeDue = Double.parseDouble(sChangeDue);

    if(dChangeDue < 0) {
        int confirmation = JOptionPane.showConfirmDialog(null, "The user has not paid the full amount. Proceed?", "Incomplete Payment", JOptionPane.YES_NO_OPTION);
        if (confirmation == JOptionPane.YES_OPTION) {
            ProceedWithSale();
        } else {
            JOptionPane.showMessageDialog(null, "Please prompt the user to complete the payment, and then proceed.", "Proceed With Payment", JOptionPane.INFORMATION_MESSAGE);
        }
    } else {
        ProceedWithSale();
    }
}

以下是ProceedWithSale()的代码:

public void ProceedWithSale() {
    DecimalFormat df = new DecimalFormat("#.00");

    //Get tax rate
    String TaxRate = "";

    try {
        Class.forName("org.h2.Driver");
        Connection connection = DriverManager.getConnection("jdbc:h2:./RetailApplicationDatabase;AUTO_SERVER=TRUE");

        String sql;
        Statement stmt;

        sql = "SELECT TaxRate FROM SETTINGS WHERE OnOff = 'ON'";
        stmt = connection.createStatement();
        ResultSet results = stmt.executeQuery(sql);

        while (results.next()) {
            TaxRate = results.getString("TaxRate");
        }
    } catch (ClassNotFoundException | SQLException ex) {
        JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
        ErrorMessage.setWrapStyleWord(true);
        ErrorMessage.setLineWrap(true);
        JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ErrorPane.setPreferredSize(new Dimension(400, 200));
        JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
    }

    double TaxRateAmount = Double.parseDouble(TaxRate);

    DefaultTableModel model = (DefaultTableModel) SaleItemTable.getModel();
    int RowCount = model.getRowCount();

    String TicketNo;
    String CustomerID;
    String InventoryNo;
    String Description;
    String Cost;
    String Retail;
    String IndividualSubTotal;
    String IndividualTax = "";
    String IndividualTotal = "";
    String SaleSubTotal;
    String SaleTax;
    String SaleTotal;

    for (int i = 0; i < RowCount; i++) {
        TicketNo = TicketNoNumberLabel.getText();
        CustomerID = CustomerIDLabel.getText().substring(13);

        InventoryNo = String.valueOf(SaleItemTable.getValueAt(i, 0));

        Description = String.valueOf(SaleItemTable.getValueAt(i, 1));

        Cost = String.valueOf(SaleItemTable.getValueAt(i, 2));

        Retail = String.valueOf(SaleItemTable.getValueAt(i, 3));

        IndividualSubTotal = String.valueOf(SaleItemTable.getValueAt(i, 3));
        double dIndividualSubTotal = Double.parseDouble(IndividualSubTotal);
        double dIndividualTax = dIndividualSubTotal * TaxRateAmount;

        String sIndividualTax = df.format(dIndividualTax);
        double dIndividualTotal = dIndividualSubTotal + dIndividualTax;

        String sIndividualTotal = df.format(dIndividualTotal);

        SaleSubTotal = SubTotalAmountLabel.getText();
        SaleTax = TaxAmountLabel.getText();
        SaleTotal = AmountDueAmountLabel.getText();

        java.util.Date javaDate = new java.util.Date();
        java.sql.Date SQLTransactionDate = new java.sql.Date(javaDate.getTime());

        DatabaseRecords SaveSale = new DatabaseRecords();
        try {
            SaveSale.SaveSale(TicketNo, CustomerID, InventoryNo, Description, Cost, Retail, IndividualSubTotal,
                    sIndividualTax, sIndividualTotal, SaleSubTotal, SaleTax, SaleTotal, SQLTransactionDate);
        } catch (ClassNotFoundException | SQLException ex) {
            JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
            ErrorMessage.setWrapStyleWord(true);
            ErrorMessage.setLineWrap(true);
            JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            ErrorPane.setPreferredSize(new Dimension(400, 200));
            JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    JOptionPane.showMessageDialog(null, "The sale has been saved.", "Sale Saved", JOptionPane.INFORMATION_MESSAGE);

    CustomerSearchTextField.setEnabled(false);
    InventoryNoTextField.setEnabled(false);
    DescriptionTextField.setEnabled(false);
    CostTextField.setEnabled(false);
    RetailTextField.setEnabled(false);
    AddButton.setEnabled(false);
    RemoveSelectedItemButton.setEnabled(false);
    SaleItemTable.setEnabled(false);
    CompleteSaleButton.setEnabled(false);
    TenderedTextField.setEnabled(false);
    CancelButton.setText("Finish");

    DatabaseRecords IncreaseSaleTicketNumber = new DatabaseRecords();
    try {
        IncreaseSaleTicketNumber.IncreaseSaleTicketNoNumbering(TicketNoNumberLabel.getText());
    } catch (ClassNotFoundException ex) {
        JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
        ErrorMessage.setWrapStyleWord(true);
        ErrorMessage.setLineWrap(true);
        JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ErrorPane.setPreferredSize(new Dimension(400, 200));
        JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
    } catch (SQLException ex) {
        JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
        ErrorMessage.setWrapStyleWord(true);
        ErrorMessage.setLineWrap(true);
        JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ErrorPane.setPreferredSize(new Dimension(400, 200));
        JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
    }

    SaleReceipt Receipt = new SaleReceipt();
    Receipt.InitializeReceipt(TicketNoNumberLabel.getText());
}

我不确定是什么导致我的数据库中的信息恢复到原始数量。

编辑:02/02/2015(MST上午8:33) -

AddQuantityToInventory

public void AddQuantityToInventory(String InventoryNo) throws ClassNotFoundException, SQLException {
    Class.forName("org.h2.Driver");
    Connection connection = DriverManager.getConnection("jdbc:h2:./RetailApplicationDatabase;AUTO_SERVER=TRUE");
    connection.setAutoCommit(true);

    String sql;
    Statement stmt;
    PreparedStatement prepare;

    sql = "SELECT Quantity FROM Inventory WHERE InventoryNo = '" + InventoryNo + "';";
    stmt = connection.createStatement();

    ResultSet results = stmt.executeQuery(sql);

    String currentQuantity = "0";

    while(results.next()) {
        currentQuantity = results.getString("Quantity");
    }

    int newQuantity = Integer.parseInt(currentQuantity) + 1;

    sql = "UPDATE Inventory SET Quantity = '" + newQuantity + "' WHERE InventoryNo = '" + InventoryNo + "';";
    prepare = connection.prepareStatement(sql);
    prepare.execute();
}

RemoveQuantityFromInventory

public void RemoveQuantityFromInventory(String InventoryNo) throws ClassNotFoundException, SQLException {
    Class.forName("org.h2.Driver");
    Connection connection = DriverManager.getConnection("jdbc:h2:./RetailApplicationDatabase;AUTO_SERVER=TRUE");
    connection.setAutoCommit(true);

    String sql;
    Statement stmt;
    PreparedStatement prepare;

    sql = "SELECT Quantity FROM Inventory WHERE InventoryNo = '" + InventoryNo + "';";
    stmt = connection.createStatement();

    ResultSet results = stmt.executeQuery(sql);

    String currentQuantity = "0";

    while(results.next()) {
        currentQuantity = results.getString("Quantity");
    }

    int newQuantity = Integer.parseInt(currentQuantity) - 1;

    sql = "UPDATE Inventory SET Quantity = '" + newQuantity + "' WHERE InventoryNo = '" + InventoryNo + "';";
    prepare = connection.prepareStatement(sql);
    prepare.execute();
}

编辑:02/02/2015(美国夏令时上午8:40) - 以下是InventoryNoTextField焦点丢失时的代码。我不认为这是问题所在,因为当我'Tab'到另一个字段时,信息不会恢复。但是,我认为这可能是有价值的信息,所以我添加了它。

InventoryNoTextFieldFocusLost

private void InventoryNoTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                               
    String InventoryNo = InventoryNoTextField.getText();

    try {
        Class.forName("org.h2.Driver");
        Connection connection = DriverManager.getConnection("jdbc:h2:./RetailApplicationDatabase;AUTO_SERVER=TRUE");

        String sql;
        Statement stmt;

        sql = "SELECT * FROM Inventory WHERE InventoryNo = '" + InventoryNo + "';";
        stmt = connection.createStatement();

        ResultSet results = stmt.executeQuery(sql);

        while(results.next()) {
            DescriptionTextField.setText(results.getString("Description"));
            CostTextField.setText(results.getString("Cost"));
            RetailTextField.setText(results.getString("Retail"));
        }

    } catch (ClassNotFoundException | SQLException ex) {
        JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
        ErrorMessage.setWrapStyleWord(true);
        ErrorMessage.setLineWrap(true);
        JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ErrorPane.setPreferredSize(new Dimension(400, 200));
        JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
    }
}

编辑:02/02/2015(12:15 PM) - 我将commit语句添加到两个数据库记录方法中以更新数量。

0 个答案:

没有答案