wood burning stoves 2.0*
The moose likes JDBC and the fly likes How to create a DB table from legacy Java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to create a DB table from legacy Java code?" Watch "How to create a DB table from legacy Java code?" New topic
Author

How to create a DB table from legacy Java code?

John Cougar
Greenhorn

Joined: Aug 15, 2009
Posts: 2
I was handed a legacy Java application's source code that does some DB operations. However, I did not get the DB tables/structures/SQL scripts.

I know which Java file contains the fields that map to fields in the DB. It's just a class with member variables like so:


Is there any [non-manual] way of generating a table out of the Java class that contains just these variables?

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Can you parse the Java file and use the field names to create a table? It might be quicker by hand, unless you have thousands of fields, in which case your table will be unconscionably large.

And welcome to JavaRanch
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

John,
Where is the legacy application running now? Can you export the schema (or data) from there and import it in the new place?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
John Cougar
Greenhorn

Joined: Aug 15, 2009
Posts: 2
I was hoping I wouldn't have to parse the Java file and that there'd be a simpler, easier approach

Unfortunately I have no access to the old database or its schema. The application was "retired" several years ago, and "they're" trying to revive it now.

In the absence of any "automated" approach, I was thinking of parsing the Java file and generate a SQL script out of it.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

Hmm. Interesting problem.

I wonder if hbm2ddl would meet your needs. Even if you aren't using Hibernate, you could download the jar and add the annotations temporarily. Might be faster than writing the script yourself. Or might not - depends on how easy your file is to parse.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I agree. Hibernate annotations can come in handy in this situation. It may well be the easiest way to go.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to create a DB table from legacy Java code?
 
Similar Threads
TableModel using a HashMap<Integer, Room>
How to manage empty entities and/or entities with no tables using discriminators/inheritance
singleton designpattern
axis2/webshpere response != axis2/glassfish response........ why?
What is the best practice to store an object graph?