Click here to Skip to main content
15,891,726 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Do you want string or string? Pin
KP Lee14-Feb-12 14:30
KP Lee14-Feb-12 14:30 
GeneralRe: Do you want string or string? Pin
Brisingr Aerowing31-Mar-12 7:04
professionalBrisingr Aerowing31-Mar-12 7:04 
GeneralBad filtering Pin
Jtai6-Feb-12 8:12
Jtai6-Feb-12 8:12 
GeneralRe: Bad filtering Pin
ekolis6-Feb-12 10:35
ekolis6-Feb-12 10:35 
GeneralRe: Bad filtering Pin
Jtai6-Feb-12 15:04
Jtai6-Feb-12 15:04 
JokeRe: Bad filtering Pin
AspDotNetDev6-Feb-12 15:24
protectorAspDotNetDev6-Feb-12 15:24 
GeneralRe: Bad filtering Pin
KP Lee14-Feb-12 17:34
KP Lee14-Feb-12 17:34 
RantDAO Framework PinPopular
Chanoch Wiggers1-Feb-12 2:06
Chanoch Wiggers1-Feb-12 2:06 
tecgoblin wrote:
It's surprising how many people try to create their own solutions to problems which already have established and well-behaving solution :S.


In the spirit of this comment I wanted to introduce a new standard - the DAO Framework. Problem: "Some day you may wish to change databases." Solution: "Create a generic database access framework to ease migration."

Example:

Firstly, define a DBConst file as follows:

public class DBConst {
  public static String SELECT = "SELECT";
  public static String WHERE = "WHERE";
  public static String AND = "AND"
  etc
}


Then create an interface: SQLFactory as follows:

public interface SQLFactory {
  public String createSQL(String[] selectors, String[] whereClauses);
}


If I remember correctly, now all you have to do is (Not in the original this was a class, I invented using interfaces - yay me):

Java
public interface MyTable {
  public static String TABLE_NAME = "MyTable";
  public static String COL_1 = "Col1";
  public static String COL_2 = "Col2";
}


Okay, we're nearly ready to write some code:

public class MyTableDao {
  public MyObject selectObject(String selector) {
    // list the columns to select
    String[] columns = {MyTable.COL_1, MyTable.COL_2);
    Object[] types = {DbTypes.DATE, DbTypes.INTEGER};

    // list the selector(s) for the statement "WHERE COL_1 = ?"
    String[] whereClause = {MyTable.COL_1};
    Object[] whereValues = {selector};

    MySqlFactory sqlFactory = new MySqlFactory();
    String sql = sqlFactory.createSql(columns, whereClause);

    PreparedStatement ps = connect.prepareStatement(sql)

    // this might have been a generic utility actually. Overridden 
    // for complex requirements maybe?
    MySqlInjector.injectValues(ps, new Object[] {selector});

    // get the resulting data and create objects from it
    ResultSet rs = stmt.executeQuery(query);
    MyObject[] objs = MyObjectFactory.getObjects(rs, columns, types);

}


Yeah, I think that was it. I'm writing from memory from 5 years ago so forgive if errors.

Key standards:
* List the columns to retrieve
sb.append(DbConsts.SELECT).append(" ").append(COL).append(,) ...

* List the types of the columns so an object factory can retrieve the right type
if(type[0]==DbTypes.INTEGER) rs.getInt(); // or whatever

* List the WHERE clauses
sb.append(DbConsts.WHERE).append(" ").append(clauses[0]...



I forgot about the static object factories until now. I suspect the SQL factories were static rather than implementations of an interface. It was more by program by convention.

The rest of the implementation is left as an exercise for the reader.

Can we stop all this nonsense with Hibernate now please?
GeneralRe: DAO Framework Pin
GibbleCH1-Feb-12 5:58
GibbleCH1-Feb-12 5:58 
GeneralRe: DAO Framework Pin
Chanoch Wiggers1-Feb-12 11:27
Chanoch Wiggers1-Feb-12 11:27 
GeneralRe: DAO Framework Pin
JackDingler14-Feb-12 9:12
JackDingler14-Feb-12 9:12 
GeneralRe: DAO Framework Pin
Sander Rossel1-Feb-12 11:50
professionalSander Rossel1-Feb-12 11:50 
GeneralRe: DAO Framework Pin
Baxter R Pearson1-Feb-12 21:25
Baxter R Pearson1-Feb-12 21:25 
GeneralRe: DAO Framework Pin
Chanoch Wiggers1-Feb-12 22:42
Chanoch Wiggers1-Feb-12 22:42 
GeneralRe: DAO Framework Pin
Baxter R Pearson2-Feb-12 1:14
Baxter R Pearson2-Feb-12 1:14 
GeneralRe: DAO Framework Pin
Kirk Wood7-Feb-12 2:10
Kirk Wood7-Feb-12 2:10 
GeneralRe: DAO Framework Pin
ekolis3-Feb-12 14:59
ekolis3-Feb-12 14:59 
GeneralRe: DAO Framework Pin
User 48220337-Feb-12 0:57
User 48220337-Feb-12 0:57 
GeneralRe: DAO Framework Pin
TylerMc0077-Feb-12 8:22
TylerMc0077-Feb-12 8:22 
GeneralRe: DAO Framework Pin
Rob Grainger12-Feb-12 14:21
Rob Grainger12-Feb-12 14:21 
GeneralRe: DAO Framework Pin
cpkilekofp7-Feb-12 8:36
cpkilekofp7-Feb-12 8:36 
GeneralRe: DAO Framework Pin
ClockMeister7-Feb-12 13:11
professionalClockMeister7-Feb-12 13:11 
GeneralRe: DAO Framework Pin
HuntrCkr7-Feb-12 17:36
HuntrCkr7-Feb-12 17:36 
GeneralRe: DAO Framework Pin
Florin Jurcovici7-Feb-12 21:46
Florin Jurcovici7-Feb-12 21:46 
GeneralRe: DAO Framework Pin
CDP18027-Feb-12 23:41
CDP18027-Feb-12 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.