1

您好,我正在尝试创建一个具有用户的程序,并且每个用户都可以根据他的每小时工资值和他工作了多少小时来计算他的付款。

我堆叠在为特定用户增加小时数或更新的按钮上

usernameGL --> 全局变量 (public static String usernameGL ="";) Connect() --> 方法(有效!)

我正在使用 Access DB 和 java

    private void addBT_actionPerformed(ActionEvent e) {
    double hours1 = 0.0;
    double hoursTable = 0.0;

    hours1 =
            Double.parseDouble(JOptionPane.showInputDialog(this, "How many hours did u work?", JOptionPane.QUESTION_MESSAGE));

    //establish connection
    Connect();

    try {
        ResultSet result;
        Statement state = conn.createStatement();
        String SQL = "SELECT Att.hours FROM Att WHERE Att.ID = users1.ID AND users1.username='" + usernameGL + "'";
        result = state.executeQuery(SQL);

        if (result.next()) {
            hoursTable = result.getDouble("hours");
            if (hoursTable == 0) {
                SQL = "INSERT INTO Att (hours) VALUES (" + hours1+")";
                result = state.executeQuery(SQL);

                String hoursString = toString().valueOf(hoursTable);
                currentTF.setText(hoursString);

            } else if (hoursTable > 0) {
                double total = hoursTable + hours1;
                SQL = "UPDATE Att SET hours= (" + total + ") WHERE hours = (" + hoursTable+")";

                String hoursString = toString().valueOf(total);
                currentTF.setText(hoursString);
            }

        }


    } catch (SQLException f) {
    }
}
4

0 回答 0