-2

ログインUIがあります。ログイン時に、ユーザー 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()

ステップ 2: datetime と user_id をログ テーブルに保存する

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