我有一個登錄用戶界面。當登錄時執行用戶ID和登錄ID插入到日誌表中。 用戶界面有用戶名字段,密碼字段和分支字段。當名稱,密碼,分支是填充並按登錄按鈕用戶ID和logdate插入到日誌表。我怎麼能解決這個問題。在記錄表從MySQL數據庫的名稱字段保留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);
}
}
那你已經嘗試過? – Robin
String sql =「插入日誌值(?,?,?)」; 字符串sql1 =「從用戶選擇*」; 嘗試{ Statement stm = con.createStatement(); // ResultSet rs = stm.executeQuery(sql1); // while(rs.next()) {User u = new User(rs.getInt(1)); //返回你; } PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,userid); ps.setTimestamp(2,logtime); ps.setInt(3,getRecord()); ps.executeUpdate(); ps.close(); con.close(); – Snahar