0

I am saving date logs every time a user logs in. Date logs will save a date in the record, but what I want is for there to be no duplicates. It will check if the record is already existing, and if yes it will not save the date. If the record does not yet exist it will save the date.

Here is my code:

 SimpleDateFormat dnow2 = new SimpleDateFormat("MMMMM dd, yyyy");

 ResultSet ds = MyDB.rsFetch("SELECT * FROM `date-logs` WHERE `Date` = '"+dnow.format(new java.util.Date())+"'");

 try {
   ds.next() ; 

   if (ds.getMetaData().getColumnCount() == 0) {
     MyDB.exSQL(  "INSERT INTO `date-logs` (`Date Code`, `Date`) " 
                + "VALUES ('"+dnow.format(new java.util.Date())+ "',"
                + "'"+dnow2.format(new java.util.Date())+"')");   
   }
 } catch (SQLException ex) {
   Logger.getLogger(FrmLogIn.class.getName()).log(Level.SEVERE, null, ex);
 }
5
  • 3
    So what is your question? Commented Feb 21, 2013 at 1:10
  • if there existing record to the database it will not save anymore if. new record it will save. when i run this nothing happens can you help me Commented Feb 21, 2013 at 1:47
  • 1
    Have you checked if your SELECT query is working properly, i.e if it is actually returning any result. Looks like you are formatting your date as "MMMMM dd, yyyy" which evaluates to 'February 21, 2013'. Can you check if the query SELECT * FROM date-logs WHERE Date = 'February 21, 2013' is retriving any result. Commented Feb 21, 2013 at 2:49
  • yes its working properly Commented Feb 21, 2013 at 3:29
  • all I just want to happen if the record is already existing it will not save anymore if, if new record it will save Commented Feb 21, 2013 at 3:30

3 Answers 3

1

Finally I got it !!!

 SimpleDateFormat dnow2 = new SimpleDateFormat("MMMMM dd, yyyy");

    ResultSet ds = MyDB.rsFetch("SELECT * FROM `date-logs` WHERE `Date Code` = '"+dnow.format(new java.util.Date())+"'");


  int resultsCounter = 0;  
    try {
        while(ds.next())  
        {  
        //procedure when results were returned  
        resultsCounter++;  
        }

        if (resultsCounter ==0)  
        {  

      MyDB.exSQL("INSERT INTO `date-logs` (`Date Code`, `Date`) VALUES ('"+dnow.format(new java.util.Date())+ "',"
                     + "'"+dnow2.format(new java.util.Date())+"')");
        }  
    } catch (SQLException ex) {
        Logger.getLogger(FrmLogIn.class.getName()).log(Level.SEVERE, null, ex);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know which database abstraction stuff you're using, but you should update it to use prepared statements.

Next, you should use MySQL specific query, like:

insert into date logs ... on duplicate key ignore 

make sure that Date is primary key. This kind of query will ignore inserts if the key already exists in the database.

4 Comments

if I make that primary then when another user will login it will cause an error
According to your example, you are not storing any user-specific information in logs table, so it doesn't matter. If it does - you may use compound primary key on username and date columns.
MyDB.exSQL( "INSERT INTO date-logs (Date Code, Date) " + "VALUES ('"+dnow.format(new java.util.Date())+ "'," + "'"+dnow2.format(new java.util.Date())+"')");
date-log is in the other table so when one user will log in... this code will run
0

What you should be doing is to add another foreign key for user (make it primary as well) to the date-logs table. Even if you insert the same data twice it will ignore the query since the primary key already exists in the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.