0

I am working on a personal project creating a punch clock in java. Since there will be several users clocking in and out I created a JTextField where users will type their ID number and when they click on the button(jButton1) the program should do two things.

  1. should go through the column of the database JDBC looking for the ID number and
  2. Once the ID number is found the program should copy the current time into the record for clock in or out, otherwise display an error message such as "ID number not found".

This is the idea, I have learnt some things on my own and with the help of google. However, I cannot find this part, any help/direction at all is appreciated. Thank you.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
Bart g
  • 587
  • 2
  • 13
  • 28
  • Honestly I have no idea how to create something like => if 007485378 = 007485378(in database) then copy current time. I have been looking in google but I cannot really find something useful. – Bart g Jun 07 '12 at 06:56

1 Answers1

1

You should use a SwingWorker to perform the database operation. Since you are already using JDBC, use PreparedStatement and execute your query to fetch result.

If result is found update database with current time, you can get current time in millisecond by calling System.currentTimeInMillis()

Once your worker is done its operation, then display the appropriate message to the UI.

1.How to use PreparedStatement

2.A sample implementation for using SwingWorker for database operation

Considering ID is coming from a JTextBox, use PreparedStatement like this:

String sql = "SELECT FIRST_NAME, LAST_NAME, HOURS FROM WORKERS WHERE ID= ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstml.setInt(1, 007859378);
ResultSet rs = pstmt.executeQuery();

while(rs.next()){
   //Extract values from ResultSet
}

This way you will prevent yourself from SQL Injection, also read how Prepared Statement helps against SQL Injection

Community
  • 1
  • 1
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • Ok, thank you very much. I think I had seen this option before. – Bart g Jun 07 '12 at 07:39
  • Hi, don't know if this is the best way to ask you a question. But I have been working on my question and I came up with this idea, what do you think?? String sql="SELECT FIRST_NAME, LAST_NAME, HOURS FROM WORKERS WHERE ID=007859378" rs=stmt.executeQuery(sql); rs.next(); String first = rs.getString("First_Name"); String last = rs.getString("Last_Name"); String id = Integer.toString(id_col); textID.sestText(id); textFirstName.setText(first); textLastName.setText(last); I am sorry I don't know how to keep the indentation, it does not look good, hope you understand. Thank you – Bart g Jun 07 '12 at 10:07
  • YOU ARE AWESOME THANK YOU VERY MUCH FOR YOUR HELP. Ok I will read what you asked me to read. – Bart g Jun 07 '12 at 10:17
  • No worries :), glad to be of help – mprabhat Jun 07 '12 at 10:17