-2

我有一个登录用户界面。当登录时执行用户 ID 和登录 ID 插入到日志记录表中。UI 有用户名字段、密码字段和分支字段。当名称、密码、分支被填写并按下登录按钮时,用户 ID 和日志日期插入到日志表中。我怎么能解决这个问题。

在记录表中插入表............

public User  insertuserloginDate(Connection con,int userid,Timestamp logtime){

    String sql="INSERT into logging values(?,?,?)";
    String sql1="Select * from user";

    try {

           Statement stm=con.createStatement();
    //               ResultSet rs=stm.executeQuery(sql1);
    //               while (rs.next()){
                       User u=new User(rs.getInt(1));
   //                  return u;
                     }

        PreparedStatement ps=con.prepareStatement(sql);
        ps.setInt(1, userid);
        ps.setTimestamp(2, logtime);
        ps.setInt(3, getRecord());
        ps.executeUpdate();
        ps.close();
        con.close();

    }  catch (SQLException ex) {
            System.out.println("Error while login record " + ex);

}
   return null;

并在按钮动作中............

private void buttonloginActionPerformed(java.awt.event.ActionEvent evt) {   

    char[] temp_pwd = userpassword.getPassword();
    String pwd = null;
    pwd = String.copyValueOf(temp_pwd);
    System.out.println("password " + pwd);

    //try {
    // TODO add your handling code here:
    if (user.loginApplication(connect.getCon(), username.getText(),
            pwd, userbranch.getSelectedItem().toString())) {

        System.out.println("success ");
        MainForm mainForm = new MainForm();
        mainForm.setVisible(true);

        user.insertuserloginDate(connect.getCon(), user.getUid(), user.getLogdate());
        System.out.println("record user log time " +user.getUid());
       // user.getuser(connect.getCon());

    }else {
        JOptionPane.showMessageDialog(null, "Login Failed!", "Failed!",
                JOptionPane.ERROR_MESSAGE);
    }
}   
4

1 回答 1

1

您可以执行以下 2 个步骤:

step1:检查用户名和密码组合是否正确,从数据库中获取user_id

sql ="select id from user_table where username='%s'" %(username)
user_id = db.exexute(sql).fetchone()

step2:将日期时间和用户 ID 保存到您的日志记录表中

now = datetime.now()
sql = "insert into log_table values (%d,'%s')".%(user_id,now)
db.execute(sql)
于 2013-10-12T07:56:52.023 回答