This discussion is archived
3 Replies Latest reply: Oct 23, 2008 2:48 PM by 843785 RSS

DB Record DML Style - Where should the DML go?

843785 Newbie
Currently Being Moderated
Situation:
I have a class that represents a record in the DB.
I have another class, potentially many classes, that instantiates the "record" class.

Q1: When inserting a record object into the DB, should the insert code be in the record class or the class that instantiates the record class?

Q2: Where should the JDBC/connection details be?

Thanks,

Gregory
  • 1. Re: DB Record DML Style - Where should the DML go?
    843785 Newbie
    Currently Being Moderated
    My way of thinking goes along this line:

    Record Class--Just holds the data:
    Class that instantiates a Record Object--any class that needs to use it.
    JDBC Connection--separate class that is instantiated as needed and visible to all classes that use Record Class.

    Then the actuall interface details can be hidden from the end user:
    String dbName = "mydb";
    MyRecord r = new MyRecord();
    //access the setters for MyRecord as needed
    MyConnection2DB c = new MyConnection2DB(dbName);
    try{
      c.add(r);
    }catch(//exceptions as needed){
      //handle exceptions as needed
    }
  • 2. Re: DB Record DML Style - Where should the DML go?
    843785 Newbie
    Currently Being Moderated
    Record Class--Just holds the data:
    Class that instantiates a Record Object--any class that needs to use it.
    JDBC Connection--separate class that is instantiated as needed and visible to all classes that use Record Class.
    Keeping the JDBC Connection class separate makes sense.

    So, are you suggesting that the insert, update, or delete action is done by the class that is using the record class and not the record class itself?
    Is that because the connection is NOT in the record class?

    Thanks,
    Gregory
  • 3. Re: DB Record DML Style - Where should the DML go?
    843785 Newbie
    Currently Being Moderated
    CanisPolaris wrote:
    Record Class--Just holds the data:
    Class that instantiates a Record Object--any class that needs to use it.
    JDBC Connection--separate class that is instantiated as needed and visible to all classes that use Record Class.
    Keeping the JDBC Connection class separate makes sense.

    So, are you suggesting that the insert, update, or delete action is done by the class that is using the record class and not the record class itself?
    Is that because the connection is NOT in the record class?

    Thanks,
    Gregory
    Yes, and yet not at all: what I suggest is that you make it part of your Connection Class, where I have add(record), you will also need a remove(record), and get(record). You may want to overload your "add" method to distinguish between insert and update, but I found making an "upsert"--update/insert type of stored proc that user doesn't have to worry about the details for, is much nicer from their perspectives.