我似乎无法找到获取自动生成密钥的正确代码。 以下是我的代码:
private void btnLogActionPerformed(java.awt.event.ActionEvent evt) {
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String sql ="select * from tblaccount where Username=? and Password=?";
这是登录帐户
try{
pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1,txtUser.getText());
pst.setString(2,txtPass.getText());
rs=pst.executeQuery();
登录后让字符串在另一个窗口显示。
if (rs.next()){
int id = rs.getInt("UserID");
PersonnelAccount.UserID = id;
String username = rs.getString("Username");
PersonnelAccount.Username = username;
String name = rs.getString("Name");
PersonnelAccount.Name = name;
记录用户进入数据库的时间
String sql1 = "Insert into tbltime (Username,date,TimeIn,TimeOut) values(?,?,?,?)";
("Insert into tbltime (Username,date,TimeIn,TimeOut) values (?,?,?,?)");
pst1=(PreparedStatement)conn1.prepareStatement(sql1,Statement.RETURN_GENERATED_KEYS) ;
pst1.setString(1,txtUser.getText());
pst1.setString(2,sdf1.format(cal.getTime()));
pst1.setString(3,sdf2.format(cal.getTime()));
pst1.setString(4,"0000/00/00 00:00:00");
pst1.executeUpdate();
rs1=pst1.getGeneratedKeys();
if(rs1.next()){
我想我在这里得到了错误...我想获取自动生成密钥,以便我可以更新Timeout数据库。所以我想获得TimeID
String time = rs1.getString(+rs1.getInt(1));
PersonnelAccount.TimeID = time;
setVisible(false);
P_Loading ob=new P_Loading();
ob.setUpLoading();
ob.setVisible(true);
}
}
else{
JOptionPane.showMessageDialog(null,"Incorrect Username and/ Password");
txtUser.setText("");
txtPass.setText("");
}
} catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
finally {
try{
rs1.close();
pst1.close();
rs.close();
pst.close();
} catch(Exception e){