• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the best design approach when querying a database in a web app?

 
Adrian Kan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I've been hearing all along
Use jsp to display information, use servlet to handle post and response and JavaBeans to process business logics

So if my web application requires the use of database, where should I put my code to query the database
I've seen samples of code and all codes to connect and query the database are put in the serlvet doPost or doGet methods
It seems a bit messy to me, but what's the best design approach to do this?

And is there a sample?


Thanks
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adrian,

as you have guessed correctly putting database access code directly into servlet service methods or JSP scripts is bad and ugly

Unfortunately it's harder to tell anything like a "best way". There are fundamentally different access methods like pure JDBC with SQL statements or O/R mappers like hibernate for example. Despite this technology difference you could still hide this in something like repositories which are used in the context of domain driven development. If your application is large or important enough to create a layered architecture you will (or at least should) have a separate layer for persistence functionality. It gets still more complicated if you used any kind of framework like Spring or Seam for example which offer you even more advanced features to "hide" database access.

So it's definitely too hard to give really concrete advices if you don't have more specific requirements which technologies and methods would be acceptable and which not...

Marco
 
Campbell Ritchie
Sheriff
Posts: 51346
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would sit better on our JDBC forum. Moving thread.
 
Campbell Ritchie
Sheriff
Posts: 51346
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have already seen the Java™ Tutorials section and the java.sql package summary, which has notes links at the end of the page.
 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you need reusable class use separate java class to database connection code,java class mean not a servlet.normal java class.
then use that class to call database.
then if you need to improve performance you can use stored procedures,user definde functions,indexing............
that's depend to you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic