aspose file tools*
The moose likes JDBC and the fly likes What is the preferred way of coding the database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the preferred way of coding the database?" Watch "What is the preferred way of coding the database?" New topic
Author

What is the preferred way of coding the database?

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Which way do you prefer to code the Database?

1)


2)




Thanks
Jack
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1775
    
  14

Don't do "SELECT * FROM customer".

It's quite rare that you really need every column from every record in a given table for a DB application, and fetching unnecessary data just adds extra work and uses up bandwidth. Select specific columns, and only for the records you want i.e. use a WHERE clause wherever possible.

Another problem with "SELECT * ..." is that the table structure may change. Columns may be added/removed from the table, or the column names/data types might change, but there's nothing in your SQL statement to tell the next programmer whether they need to change your SQL to take account of these changes. Make your intentions for your SQL query explicit - select specific columns by name - so that maintaining the program is easier when (not if) the DB changes.


No more Blub for me, thank you, Vicar.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Along with the good advice from Chris, the style of option 2 is definitely the way to go. You don't want calling code to have to know about SQL strings or result sets. It simply needs to provide the ID of the customer to retrieve and the method does the rest.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
got it, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the preferred way of coding the database?