Blog do projektu Open Source JavaHotel

niedziela, 27 lutego 2011

DB2, Spring and GWT

I created simple GWT (2.2)  application trying to put together several different technologies :
DB2 (SAMPLE database), Spring JDBC as a layer between user interface and database (persistence) and GWT 2.2 as a user interface.
How to setup environment - read details from SampleGWT and source code.

I ended up with the following application:



Application displays one table (EMPLOYEE) from SAMPLE database. List box at the upper left of the screen allows data sorting - it is done by adding ORDER BY clause to SELECT * FROM EMPLOYEE statement. 'Incremental' check box does nothing with the statement - data are read incrementally by default. 'Decremental' check box adds 'DESC' clause to ORDER BY.
Of course - sorting can be done also by 'Column Sorting' - new feature added in GWT 2.2 to CellTable presentation widget.
It could be very interesting challenge which sorting method is more efficient. I dare say that more data are used than data sorting by database engine will be getting the upper hand.

I used Spring JDBC for data access layer. Of course - for such a small project (one statement) it is like to take a sledgehammer to crack a nut.
But I found it very useful - it is much more elegant to have in the main business stream something like:
  List mList = jdbc.query(query, new EmployeeRecordMapper());
and keep somewhere else :
private class EmployeeRecordMapper implements RowMapper {
        @Override
        public EmployeeRecord mapRow(ResultSet res, int arg1)
                throws SQLException {
            String empno = res.getString(1);
            String firstname = res.getString(2);
            String midinit = res.getString(3);
            String lastname = res.getString(4);
            String workdept = res.getString(5);
            String phoneno = res.getString(6);
            Timestamp hiredate = res.getTimestamp(7);
            String job = res.getString(8);
            int edlevel = res.getInt(9);
            String sex = res.getString(10);
            Timestamp birthdate = res.getTimestamp(11);
            BigDecimal salary = res.getBigDecimal(12);
            BigDecimal bonus = res.getBigDecimal(13);
            BigDecimal comm = res.getBigDecimal(14);
            EmployeeRecord el = new EmployeeRecord();
            el.setEmpno(empno);
            el.setFirstname(firstname);
            el.setMidinit(midinit);
            el.setLastname(lastname);
            el.setWorkdept(workdept);
            el.setPhoneno(phoneno);
            el.setHiredate(hiredate);
            el.setJob(job);
            el.setEdlevel(edlevel);
            el.setSex(sex);
            el.setBirthdate(birthdate);
            el.setSalary(salary);
            el.setBonus(bonus);
            el.setComm(comm);
            return el;
        }
    } 

 then this terrible JDBC code :
 ResultSet res = ....
 while (res.next()) {
   ...
}
What is more important   EmployeeRecordMapper class is reusable component - can be utilized everywhere statement on EMPLOYEE table is performed.

Of course - every JDBC developer sooner or later creates his (her) own JDBC framework - during my professional life I created dozen of them. But for what purpose create next framework if Spring JDBC is already created and ready to use for free ?

Brak komentarzy:

Prześlij komentarz