简体   繁体   English

有没有办法让 JButton 在第一次按下时做一件事,第二次按下时做其他事

[英]Is there a way to have a JButton do one thing when first pressed, and do other things when it is pressed a second time

I am using prepared statements in Java to insert monthly finances into a database table.我在 Java 中使用准备好的语句将每月的财务信息插入到数据库表中。 The first time I press the button each month I want it to SQL query insert, but the other times just update in order to avoid repeats for the same month.我每个月第一次按下按钮时,我希望它进行 SQL 查询插入,但其他时间只是更新以避免同月重复。 How would I be able to do this?我怎么能做到这一点?

This is my code这是我的代码

    String dbQuery18 = "INSERT INTO weekly_finances VALUES (?,?,?,?)";
    String dbQuery19 = "INSERT INTO monthly_finances VALUES (?,?,?,?)";
    // Data to be entered 
    int weekNumber = 0;
    int monthNumber = 0;
    float weeklySum = 0;
    float monthlySum = 0;

    if (command.equals("Save"))
    {
        try
        {
            try
            {
                day = thisDay;
                weekNumber = thisWeek;
                monthNumber = thisMonth;

                PreparedStatement ps16 = myDbConn.prepareStatement(dbQuery16);
                // Passing Values into ps to fill in
                ps16.setInt(1, weekNumber);
                ResultSet res = ps16.executeQuery();
                while (res.next())
                {
                    int c = res.getInt(1);
                    weeklySum = weeklySum + c;
                };

                System.out.println("This is the weekly Sum = " + weeklySum);

                PreparedStatement ps17 = myDbConn.prepareStatement(dbQuery17);
                // Passing Values into ps to fill in
                ps17.setInt(1, monthNumber);
                ResultSet res1 = ps17.executeQuery();
                while (res1.next())
                {
                    int d = res1.getInt(1);
                    monthlySum = monthlySum + d;
                };

                // Inputting into Database 
                // Prepare Statement using Connection 
                PreparedStatement ps18 = myDbConn.prepareStatement(dbQuery18);
                // Passing Values into ps to fill in
                ps18.setInt(1, weekNumber);
                ps18.setFloat(2, weeklySum);
                ps18.setFloat(3, NULL);
                ps18.setFloat(4, NULL);         
                // Executing Update
                ps18.executeUpdate();

                PreparedStatement ps19 = myDbConn.prepareStatement(dbQuery19);
                // Passing Values into ps to fill in
                ps19.setInt(1, monthNumber);
                ps19.setFloat(2, monthlySum);
                ps19.setFloat(3, NULL);
                ps19.setFloat(4, NULL);  
                // Executing Update
                ps19.executeUpdate();

In case you want multiple buttons to have that effect, I would make a ArrayList named "pressedButtons".如果您希望多个按钮具有这种效果,我会创建一个名为“pressedButtons”的 ArrayList。 Then when you handle a click, check if the ArrayList contains the button.然后当您处理点击时,检查 ArrayList 是否包含按钮。 If not, there is your first click, and add the user to the ArrayList.如果没有,则是您的第一次单击,并将用户添加到 ArrayList。 If so, that's your second click.如果是这样,那就是您的第二次点击。

However, in your case when working with sql I just would check if the data already exists via a SQL command, and then act.但是,在您使用 sql 的情况下,我只会通过 SQL 命令检查数据是否已经存在,然后采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM