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.
- should go through the column of the database JDBC looking for the ID number and
- 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.
You should use a
SwingWorkerto perform the database operation. Since you are already using JDBC, usePreparedStatementand 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, usePreparedStatementlike this:This way you will prevent yourself from SQL Injection, also read how Prepared Statement helps against SQL Injection