2) For any database change, it takes a great deal of discipline to find all of the table names and column names used throughout your code that now functions differently. Even the slightest lack of discipline results in a bug that might not be discovered until run time. Strong typing eliminates this problem. This program (Jenny) provides the strong type checking.
For each table and view in a given database, Jenny will create a java source class file. Each of these classes will provide a collection of general purpose methods for working with the table/view. They will also provide a list of all the column names, the table/view name and an inner class called Row that can be used to manipulate a single row in the table.
Many tables have a primary key that is a non-nullable integer called "tablenameID" or sometimes "ID". If Jenny finds this, she will add extra methods to the generated source for working with the primary key.
Some of my goals:Row getRow() Row getRow( String column , String searchText ) DBResults search( String column , String searchText , String[] dataColumns ) Row[] getRows( String column , String searchText ) Row[] getAllRows() DBResults search( String[] dataColumns ) void update( String column , String searchText , Map data ) void delete( String column , String searchText ) void insert( Map data )If an ID field is found, some methods like these will also be added:
Row getRow( int id ) void delete( int id )Every class will have an inner class called Row that can provide strong type checking for every field (column) as well as methods like:
void update() void delete() void insert()The strong type checking for Row is provided by getters and setters. Suppose you have a table called Employee. Jenny will generate a class called EmployeeTable that will contain a Row class that might have the following methods:
int getEmployeeID() void setEmployeeID( int employeeID ) String getLastName() void setLastName( String lastName )Here's a sample of a business logic method that uses a Jenny generated class:
// pass in an employee ID and get back the number of tax exemptions that the employee claims private int getEmployeeExemptions( int employeeID ) { return EmployeeTable.getRow( employeeID ).getExemptions(); }This same code using plain JDBC could be 10 to 40 lines long depending on how it would be implemented. You would need to get a connection, create a statement, build your sql string, execute your statement, wade through the result set, and each of these things need try/catch/finally blocks! Don't forget to close your connection, statement and result set!
java com.javaranch.db.Jenny db.propertieswhere db.properties is a properties file that describes how Jenny should find your database. There is a sample properties file complete with in-line documentation inside jr.jar at /src/com/javaranch/db/soup.properties. From your IDE:
Bring up the class in your ide and tell it to Run.
Of course, you have to have jr.jar in your IDE's classpath; you have to define the "Main Class" (a.k.a "Target" or "command") as <path>/com.javaranch.db.Jenny; you have to define the "Program Parameters" as db.properties (where db.properties is a properties file that describes how Jenny should find your database. There is a sample properties file complete with in-line documentation inside jr.jar at /src/com/javaranch/db/soup.properties); and you have to tell it which is your "working directory" (a.k.a "Start in" or "initial folder")
Note that development of Jenny is on-going and she's currently available as a beta release. Take 'er for a spin, and stop by the JDBC forum in the Big Moose Saloon to participate in her growth.