labs / tiddlers / content / labs / lab07 / _Labs_07_Examine the ScientistJdbiDAO
  1. Open the ScientistJdbiDAO class. This class has the same methods as the JDBC equivalent, but this class is using JDBI.

  2. Take a look at the addScientist method. Note that the SQL has named placeholders rather than question marks.

    This makes the code a lot easier to work with. Anyone who has spent any time working with JDBC will tell you that it is too damn easy to modify the SQL but forget to change the placeholder indexes and suddenly your customers have email addresses for first names.

    Other than the named placeholders --- the JDBI version of addScientist method looks pretty similar to the JDBC version.

  3. Take a look at the getScientists method. That is a lot less code than the JDBC equivalent.

    The mapToBean(Scientist.class) method is doing most of the hard work for us. It is creating the Scientist objects, correlating the column names in the result set with the field names in the Scientist class, and copying the data from the result set into the objects.

    JDBI is smart enough to match up Snake_Case column names with camelCase field names, but that is it. If you use scientistId in the Scientist class, and Scientist_Num in the table then bad things will happen. That is the cost of higher-level libraries like JDBI --- they can do a lot of the work for you, but you now need to be very pedantic and consistent with your naming. There are ways of dealing with mismatches in names, but ideally you shouldn't need to.