|
YOUR FEEDBACK Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java Database SQL Compiler (for Java)
Free your Java code from SQL statements - compile them to Java classes
By: Pavel Vlasov
May. 11, 2005 05:00 PM
This article describes a SQL Compiler tool (SQLC) that generates Java classes from SQL statement and table metadata. In doing so SQLC decouples Java and SQL code and enforces a clear separation of concerns between database and Java code and division of labor between data modeler and Java developer. The article also ruminates about SQLC, O/R mapping frameworks (taking Hibernate as an example) and plain JDBC applicability in different contexts. IntroductionRecently I worked on a Java application and needed to do a lot of database interaction. I didn't want to use plain JDBC for obvious reasons (that are nonetheless described below). I had already created a data model and didn't want to do double work by mirroring the data model in Java by hand.So I looked around for a carpal tunnel friendly tool, something like the erstwhile FoxPro 2.6. To my surprise I found none. Nowadays everybody seems to care about flexibility but not simplicity. After pondering for a while I preferred creative typing over mundane and wrote a tool that generates interfaces and methods from database metamodel. Lo and behold SQLC was born! SQLC generates Java classes and interfaces from information provided by java.sql.Connection.getMetaData(), java.sql.PreparedStatement.getMetaData() and java.sql.PreparedStatement.getParameterMetaData() methods. It uses BCEL and helper classes and techniques described in XREF:ARTICLE_6207. SQLC-generated classes use Squirrel as a foundation, which minimizes the amount of generated code (see resources, don't mix up with Squirrel SQL client). You may think: "Oh, yet another O/R mapping tool, why would I waste my time reading about it?" Well, SQLC is not an O/R mapping tool at all. Like spiritualists and materialists have different answers to the question "What is primary - spirit or matter?" SQLC and O/R mapping tools offer different answers to the question "What is primary - data or objects?" SQLC treats data as primary and only as data - i.e. no behavior. For example, a credit card has no behavior per se - it's just a tuple. Behavior belongs to the objects operating with the credit card, not the credit card itself. SQLC also does something what other tools don't do - it compiles parameterized statements to Java methods. This is a short summary of SQLC's advantages:
CompilationFigure 1 shows a sample model that will be used to generate classes and interfaces. We'll create a simple banking application capable of making fund transfers and collecting service charge from accounts with a low balance. The full source code of the sample application can be downloaded. See the references below.The first step is to generate classes using an sqlc Ant task: 1.<sqlc 2.script="src/com/pavelvlasov/sqlc/samples/Bank.sql" 3. dir="sqlc_generated" 4. docDir="sqlcDoc" 5. package="com.pavelvlasov.sqlc.samples" 6. masterEngine="BankEngine" 7.> 8. <table/> 9.</sqlc> In the snippet above classes are generated using Bank.sql script. In this case a Hypersonic temporary database is created, the script is executed to create database objects, and then Java classes are generated. This approach works fine if the DDL in the script is compatible with Hypersonic. If you'd like to generate classes straight from the target database (the preferred method) then instead of the script attribute nested <connection> element shall be specified. This is a sample connection element for Oracle: 10.<sqlc 11. dir="sqlc_generated" 12. docDir="sqlcDoc" 13. package="com.pavelvlasov.sqlc.samples" 14. masterEngine="BankEngine" 15.> 16. <connection 17. driverClass="com.inet.ora.OraDriver" 18. url="jdbc:inetora:server:port:DB" 19. user="DBUSER" 20. password="DBPASSWORD"/> 21. <table/> 22.</sqlc> Please note that it uses ORANXO driver because the drivers provided by Oracle itself are not fully JDBC-compatible (see the compatibility section below). The SQLC task shown above produces .class files in the sqlc_generated directory and HTML documentation in the sqlcDoc directory using table metadata. The <table/> element matches all the tables in the database. LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||