如何使用java netbeans延迟计算/超时[关闭]

问题描述 投票:-1回答:1
if(stud.getText().equals("0"))
{


Calendar calendar = Calendar.getInstance();
                java.sql.Date ourJavaDateObject = new java.sql.Date(calendar.getTime().getTime());

        try
        {
            String sql= "INSERT INTO info_history(c_id,name,middlename,lastname,contact,age,address,checkt_in,date)values(?,?,?,?,?,?,?,?,?)";
            PreparedStatement pst = cn.prepareStatement(sql);


            String sqls = " Update  info set stud = '1' where id = "+idno.getText()+" order by id DESC ";
                    PreparedStatement psts = cn.prepareStatement(sqls);
                    psts.execute(); 
                    pst.setString(1, idno.getText());      
                    pst.setString(2, name5.getText());
              pst.setString(3, middlename5.getText());
              pst.setString(4, lastname5.getText()); 
              pst.setString(5, contact5.getText());
              pst.setString(6, age5.getText());
              pst.setString(7, address5.getText());
                pst.setString(8, time.getText());    
              pst.setDate(9, ourJavaDateObject);
              pst.execute();  
              JOptionPane op2 = new JOptionPane("SAVED!",JOptionPane.INFORMATION_MESSAGE);
              JDialog dialogs1 = op2.createDialog("SAVED!");
              dialogs1.setAlwaysOnTop(true); //<-- this line
              dialogs1.setModal(true);
              dialogs1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        info();           
        }catch(Exception e)

        {
            JOptionPane op2 = new JOptionPane("INFO NOT SAVED!",JOptionPane.INFORMATION_MESSAGE);
            JDialog dialogs1 = op2.createDialog("INFO NOT SAVED!");
            dialogs1.setAlwaysOnTop(true); //<-- this line
            dialogs1.setModal(true);
            dialogs1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialogs1.setVisible(true);
        }



//insert end
}

else if(stud.getText().equals("1"))

{
 //check out
 try
        {

                    String sql = " Update  info_history set check_outt = '"+time.getText()+"' where c_id = "+idno.getText()+" order by id DESC ";
                    PreparedStatement pst = cn.prepareStatement(sql);
                    pst.execute();


            String sqls = " Update  info set stud = '0' where id = "+idno.getText()+" order by id DESC ";
                    PreparedStatement psts = cn.prepareStatement(sqls);
                    psts.execute();

              idno.setText(null);
              name5.setText(null);
              middlename5.setText(null);
              lastname5.setText(null);
              time.setText(null);

              JOptionPane op2 = new JOptionPane("CHECK OUT SAVED!",JOptionPane.INFORMATION_MESSAGE);
              JDialog dialogs1 = op2.createDialog("CHECK OUT SAVED!");
              dialogs1.setAlwaysOnTop(true); //<-- this line
              dialogs1.setModal(true);
              info();


            }catch(Exception e)
        {
            JOptionPane op2 = new JOptionPane("INFO NOT SAVED!",JOptionPane.INFORMATION_MESSAGE);
            JDialog dialogs1 = op2.createDialog("INFO NOT SAVED!");
            dialogs1.setAlwaysOnTop(true); //<-- this line
            dialogs1.setModal(true);
            dialogs1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialogs1.setVisible(true);
        }        


}       
}

我可以添加什么来计算最近的分钟数?

java netbeans
1个回答
0
投票

Java.sql.Date扩展了java.util.Date和零小时,分钟和秒。这是针对SQL DATE yyyy-MM-dd。

小时和分钟使用java.sql.Timestamp。

    Calendar calendar = Calendar.getInstance();
    Timestamp ourJavaDateObject = new Timestamp(calendar.getTime().getTime());

    String sql= "INSERT INTO info_history(c_id,name,middlename,lastname,contact,age,"
        + "address,checkt_in,date)VALUES(?,?,?,?,?,?,?,?,?)";
    String sqls = "UPDATE info SET stud = '1' WHERE id = ? ORDER BY id DESC";
    try (PreparedStatement pst = cn.prepareStatement(sql);
                PreparedStatement psts = cn.prepareStatement(sqls)) {
        psts.setInt(1, Integer.parseInt(idno.getText()));      
        psts.executeUpdate();

        pst.setString(1, idno.getText());      
        pst.setString(2, name5.getText());
        pst.setString(3, middlename5.getText());
        pst.setString(4, lastname5.getText()); 
        pst.setString(5, contact5.getText());
        pst.setString(6, age5.getText());
        pst.setString(7, address5.getText());
        pst.setString(8, time.getText());    
        pst.setTimestamp(9, ourJavaDateObject);
        pst.executeUpdate();

        JOptionPane.showMessageDialog(null, "", "SAVED!", JOptionPane.INFORMATION_MESSAGE);
        info();           
    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, "INFO NOT SAVED!", e.getLocalizedMessage(),
                JOptionPane.INFORMATION_MESSAGE);
    }

奇怪的语法try-with-resources确保语句被关闭。

© www.soinside.com 2019 - 2024. All rights reserved.