Question

I'm still new in programming. Currently, I have a program that checks the attendance of an employee. The employee id & names are retrieved from a database. The daily attendance of an employee is saved in an attendance table in the database, with fields having only an ID,empID,dateAttended,"present","overtime". Now, I want to retrieve all the values from the attendance table. At first, I can retrieve the values fine but after I added the "a.dateAttended" and "a.Present", I get this Exception. Any suggestions on how i can resolve this?

private void attendanceView(){
    try{
        String query ="SELECT e.ID,e.firstName,e.lastName,e.position,a.dateAttended,a.Present FROM employees e INNER JOIN attendance a ON e.ID=a.empID";
        Object[][] result = connectToDB(query);

        attendanceTable.setModel(new javax.swing.table.DefaultTableModel(
            result, new String [] {"Employee ID","First Name","Last Name", "Position", "Date", "Present"}
        ) 
        {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {

                return canEdit [columnIndex];
            }
        });   
        }catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
}
Was it helpful?

Solution

From this, in the comments,

SELECT e.ID,e.firstName,e.lastName,e.position,a.dateAttended 
FROM employees e INNER JOIN Attendance a ON a.empID =e.ID 
WHERE a.dateAttended = (month of the system date)

A google search on ms access date functions led me to now(), which returns the current date and time, and month(), which returns the month number. That means you can do this:

SELECT e.ID,e.firstName,e.lastName,e.position,a.dateAttended 
FROM employees e INNER JOIN Attendance a ON a.empID =e.ID 
WHERE month(a.dateAttended) = month(now())

However, there are better ways to do this. Using functions in the where clause like that slows down production. Plus, this is only good for the current month. I suggest that you use your java code to generate two date variables. The first one is the first day of the month you want to search and the other one is the first day of the following month. Then your where clause would be this

where a.dateAttended >= the first variable
and a.datAttended < the other variable

Using query parameters would improve it even more, if access supports those. I don't use it so I don't know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top