Results 1 to 8 of 8
  1. #1
    victoreadward is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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?

  3. #3
    victoreadward is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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?
    Something like this:

    my generic class
    Java Code:
    public class GenericDataAccess<T extends BeanModel> {
         ...
    }
    In this class I have a method that generate a INSERT INTO reading the T class properties as follow:
    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;
    }
    In the code above the class name is the same of the table. Therefore, the field names proceed the same way.

    Each colunm is annoted with @Colunm indicating that is a field.
    An Example:

    Java Code:
    public class test extends BeanModel {
        @Id
        private int id;
        @Colunm
        private String desc;
        ...
        getters and setter goes here
    }
    My doubt is, how can I get the fields and values from T?

  4. #4
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    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.

  5. #5
    victoreadward is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by raqman View Post
    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.
    I know Hibernate and so forth. As a matter of fact, I have made it using Hibernate-Annotation. But, because I intend to learn more about I need to do that.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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?

  7. #7
    victoreadward is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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?
    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?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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

  1. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  2. Replies: 2
    Last Post: 11-21-2008, 05:59 PM
  3. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  4. Replies: 0
    Last Post: 06-26-2008, 07:32 PM
  5. Create UI based on HashTable
    By zoe in forum AWT / Swing
    Replies: 1
    Last Post: 08-01-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •