Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / May 2007

Tip: Looking for answers? Try searching our database.

Where to store SQL statement

Thread view: 
jsguru72 - 10 May 2007 01:59 GMT
I write a lot of JSP pages with backend classes to access MySQL
databases.  One of my biggest problems is having the SQL statements
hardcoded in class.

Everytime I have to make a slight change to the sql statement, I have
to completely recompile the entire class.  This is especially
troublesome during testing when I have to update the statements
numerous times until I get it just right.

Where should I be storing the statements?

Should I put them in a text file and read them in when needed?  I have
thought of this, but I am concerned about the overhead of opening and
reading a file just to get a SQL statement.

Just trying to find out some ideas.

Thanks.
Arne Vajhøj - 10 May 2007 02:49 GMT
> I write a lot of JSP pages with backend classes to access MySQL
> databases.  One of my biggest problems is having the SQL statements
[quoted text clipped - 12 lines]
>
> Just trying to find out some ideas.

Either use SQL in the code as now and be patient when
recompiling or look at a persistence framework (example:
hibernate) that generate most of the SQL.

Reading SQL from properties file similar to i18n would
be possible, but I think it would be very confusing
for maintenance.

Arne
tzvika.barenholz@gmail.com - 10 May 2007 08:58 GMT
> I write a lot of JSP pages with backend classes to access MySQL
> databases.  One of my biggest problems is having the SQL statements
[quoted text clipped - 14 lines]
>
> Thanks.

I would export the SQL to a txt, or better XML file. The extra
overhead of reading the disk is probably not a lot, considering that
whenever you read the sql presumably you are running a query to the
database, which probably takes >> more time than the simple file
access.

T
Bruce Lewis - 10 May 2007 19:52 GMT
> I would export the SQL to a txt, or better XML file. The extra
> overhead of reading the disk is probably not a lot, considering that
> whenever you read the sql presumably you are running a query to the
> database, which probably takes >> more time than the simple file
> access.

I second this suggestion and recommend looking at jforum for an example
of putting prepared statements in properties files.

Signature

http://ourdoings.com/
Amazingly simple photo sharing

David Harper - 10 May 2007 20:59 GMT
>> I would export the SQL to a txt, or better XML file. The extra
>> overhead of reading the disk is probably not a lot, considering that
[quoted text clipped - 4 lines]
> I second this suggestion and recommend looking at jforum for an example
> of putting prepared statements in properties files.

The OP might want to look at java.util.ResourceBundle and its sub-class
java.util.PropertyResourceBundle which provide an easy way to store and
retrieve strings such as SQL queries indexed by unique names.  They are
useful for much more than I18N.

David Harper
Cambridge, England
David Gillen - 10 May 2007 10:08 GMT
jsguru72 said:
> I write a lot of JSP pages with backend classes to access MySQL
> databases.  One of my biggest problems is having the SQL statements
[quoted text clipped - 12 lines]
>
> Just trying to find out some ideas.

Use the mysql command line client to connect to the server and run queries to
your hearts content until such time as you are happy with them, then simply pu
the correct query into your code.

D.
Signature

Fermat was right.

Lulu58e2 - 10 May 2007 16:12 GMT
> ... One of my biggest problems is having the SQL statements
> hardcoded in class.
[quoted text clipped - 4 lines]
>
><snip>

I use MySQL stored procedures: no files to maintain, optimized
execution. (Note: requires MySQL 5.0 I think).

C>
Shane Petroff - 10 May 2007 21:57 GMT
>> <snip>
>>
[quoted text clipped - 3 lines]
>
> I use MySQL stored procedures

I've used all of the options mentioned so far, and none of them is
vastly superior. Frankly though, if you find it necessary to change your
sql that often, then it is a good indication that you need to study more
sql!

One of the guidelines I try to follow now is that there shouldn't be any
complex sql 'in' an application, regardless of whether or not it has
been externalized. If you need complex sql externalize it into a view
and let the dba handle the tweaks. Unlike stored procs, views are
relatively easy to port between databases, and they have the advantage
that they are well within scope for the database experts. The complexity
is still there, it's just accessible to the people most skilled at
dealing with the database (IME, selects tend to be much more complex
than either insets, updates or deletes; YMMV).

Shane

Shane
Lew - 10 May 2007 23:13 GMT
> I use MySQL stored procedures: no files to maintain, optimized
> execution. (Note: requires MySQL 5.0 I think).

Less scalable than business logic in the business layer.  I am curious,
though, how do you rate the optimization of the execution?  Have you compared
the stored procedure approach to application-server clusters with the business
logic in the middle tier under massive parallel loads?

I should think that the middleware tier is more scalable.

I use a semantic approach to architecture that argues that business logic be
placed in the business layer, and data logic in the data layer.  It turns out
that that viewpoint simplifies the decision and leads to things like optimal
scalability and stability.

Signature

Lew

Lulu58e2 - 11 May 2007 15:40 GMT
> > I use MySQL stored procedures: no files to maintain, optimized
> > execution. (Note: requires MySQL 5.0 I think).
[quoted text clipped - 6 lines]
> --
> Lew

Yikes, you got me. I actually haven't rated the optimization or
compared various tier strategies. Stored procedures for me simply mean
not storing hard-coded SQL; however, the other suggestions (e.g.
java.util.PropertyResourceBundle) sound interesting in light of what
you've asked.

C>

----------------------------------------------------------------------
A reproof entereth more into a wise man than an
hundred stripes into a fool.                Prov.17:10


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.