|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object | +--com.javaranch.db.Jenny
Jenny the db code generator. This program reads a database and generates java source which provides strongly typed access to the database. In my opinion, there are two major problems with JDBC:
Row getRow() Row getRow( String column , String searchText ) DBResults search( String column , String searchText , String[] dataColumns ) Row[] getRow( String column , String searchText ) Row[] getAllRows() DBResults getAllRows( 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.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/jenny.properties.
- - - - - - - - - - - - - - - - - Copyright (c) 2003 Paul Wheaton
You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...
My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.
The original source can be found at JavaRanch
- - - - - - - - - - - - - - - - -
| Constructor Summary | |
Jenny()
|
|
| Method Summary | |
com.javaranch.db.Jenny.TableProcessor |
getTableProcessor(java.sql.DatabaseMetaData metaData,
java.lang.String tableName,
java.lang.String suffix,
java.lang.String idColumnName)
|
static void |
main(java.lang.String[] args)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public Jenny()
throws java.lang.Exception
| Method Detail |
public com.javaranch.db.Jenny.TableProcessor getTableProcessor(java.sql.DatabaseMetaData metaData,
java.lang.String tableName,
java.lang.String suffix,
java.lang.String idColumnName)
throws java.lang.Exception
java.lang.Exception
public static void main(java.lang.String[] args)
throws java.lang.Exception
java.lang.Exception
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||