Results 1 to 8 of 8
- 12-15-2009, 03:27 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Can anyone help me to create a INSERT INTO based on a class passed?
Hi all. I am new in Java Forums and Java language too.
I am trying to create a method that generate a SQL statement for insert data into a database.
There is an important question about. I do not know what class is coming as parameter then I cannot use getters and setters. My porpoise is code only a SQL statement and use it for all classes in my application.
Could you help me?
- 12-15-2009, 05:19 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You're not making much sense here.
Are you saying you want to somehow generate a SQL statement without knowledge beforehand of what the class is you're using as the source of your data, or what tables it's going into?
- 12-15-2009, 05:46 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Something like this:
my generic class
In this class I have a method that generate a INSERT INTO reading the T class properties as follow:Java Code:public class GenericDataAccess<T extends BeanModel> { ... }
In the code above the class name is the same of the table. Therefore, the field names proceed the same way.Java Code:public T insert(T entity) { fields = The names of declared fields of the class T; values = The values of declared fields of the class T; String sql = "INSERT INTO " + entity.getClass().getSimpleName() + "(" + fields + ") " + " VALUES(" + values + ")"; ... return T; }
Each colunm is annoted with @Colunm indicating that is a field.
An Example:
My doubt is, how can I get the fields and values from T?Java Code:public class test extends BeanModel { @Id private int id; @Colunm private String desc; ... getters and setter goes here }
- 12-15-2009, 06:52 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 37
- Rep Power
- 0
Hi.
In my opinion, you should use a reporting tool directly.
Some of them are very easy to use, and it can help you to submit data to database easily.
Regards,
raqman.RAQ Report: free Java reporting tool.
- 12-15-2009, 07:03 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
- 12-16-2009, 10:47 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Once you have the class you can use reflection to get the attributes (though how this works with the annotations I have no idea), both in name and in accessors.
You are planning on using bind variables with your sql I would guess?
- 12-16-2009, 11:15 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
No. I am planning to do more simple way as possible. Just read the properties from class, distinguish what is a declared field like in example above and what is only a property. Then I will can build my INSERT INTO statement.
If you have some idea... does not matter if with annotation or not. I was using annotation because I though would be more easy.
Could you help me?
- 12-16-2009, 11:51 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I don't really know annotations, so can't say what can and cannot be done with them. You need to identify columns, whatever the id column is, and transient stuff. In fact, you could simply mark any fields that aren't part of the table as transient. Everything else is a column. The primary key is always an attribute called "id".
That should be something you could start with. I wouldn't want to use it in anger, of course...:)
Does that make sense?
Similar Threads
-
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM -
Need help - Create an application to loop through folder of images & insert to db
By kissiffer4 in forum SWT / JFaceReplies: 2Last Post: 11-21-2008, 05:59 PM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM -
How to use Timer class to schedule a task to execute once 5 seconds have passed
By Java Tip in forum java.utilReplies: 0Last Post: 06-26-2008, 07:32 PM -
Create UI based on HashTable
By zoe in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks