This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 590
Which way do you prefer to code the Database?

1)


2)




Thanks
Jack
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1625
    
  13

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: 1012
    
    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: 590
got it, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the preferred way of coding the database?
 
Similar Threads
Retrieving database values inside the <s:select> tag
What is your general practice to generate employee IDs?
Selecting income range by SQL
oracle connection pool
Same query, different results