1

I cannot seem to find the right code of getting the autogenerate key.. Below is my code:

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=?";

this is to login the account

try{

            pst=(PreparedStatement) conn.prepareStatement(sql);
            pst.setString(1,txtUser.getText());
            pst.setString(2,txtPass.getText());

            rs=pst.executeQuery();

to get the Strings to display it on the other window when logged-in.

         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;

to record the users time in into the database

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()){

I think I got the error here... I want to get the autogenerate key so I could get to update the Timeout database. So I want to get the 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){
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
ziek214
  • 11
  • 2
  • 1
    String time = rs1.getString(+rs1.getInt(1)); because you are trying to get valuse based on 1st column which may be 1000000 and your table doesn't have that column number till 1000000 or may be your are getting -ve value try Math.abs() function to get +ve Value – Jay Shankar Gupta Feb 22 '18 at 05:29
  • Just remove the plus sign in ` String time = rs1.getString(+rs1.getInt(1));` or do this: ` String time = rs1.getString(1+rs1.getInt(1));` if you want to add 1 to the `rs1.getInt(1)` value; – cdaiga Feb 22 '18 at 05:36
  • What do you think `rs1.getString(+rs1.getInt(1))` does? – Mark Rotteveel Feb 22 '18 at 13:17
  • @MarkRotteveel this is to get the autogenerated key ["TimeID"] (Primary key) from the database "tbltime". – ziek214 Feb 22 '18 at 19:36
  • @cdaiga I did that already but the error is the same. it just keeps adding (+1) the (int) column value.. Error states "Column index out of range (int+1)>1.. – ziek214 Feb 22 '18 at 19:38
  • That is what you think, but that is not what it does. You first use `rs.getInt(1), which - probably - gets the generated id, and then you use `rs.getString()`, which is unlikely to work, as the index passed is the **column** index in the result set. – Mark Rotteveel Feb 22 '18 at 19:40
  • @JayShankarGupta Im getting numerous errors when I used that function.. – ziek214 Feb 22 '18 at 19:45
  • oh, I see.. Do you have any other codes to replace that sir? any possible solution would be of great help... @MarkRotteveel – ziek214 Feb 22 '18 at 19:53
  • See the linked duplicate. – Mark Rotteveel Feb 22 '18 at 19:54

0 Answers0