• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet to connect with MySQL database

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just starting out with some basic servlets.
I want to connect them up with a MySQL student database i also have running on my computer. The database is up and running fine and i have tested the connection.
I have a .xhtml page with radio buttons to add, delete or display students in the database. Currently i have a servlet handling the response and redirecting to three different pages.
A few questions after trying to achieve this.
Should i use multiple servlets for this? I have one that handles the request from the first form, with the radio buttons deciding which page to redirect to.
I was thinking i could then use one for the delete and one for the add form? This would just involve putting their entries into the Web.xml file?
Also what is the best way to connect the servlet with the database? I have a slimmed down java class that i used with my MySQL database. It connects with the database and adds, deletes, displays results using sql statements.
In my servlet i use the methods within this class to perform the operations, is this the best way to connect?
If it is then i encounter a problem when trying to display the results. My operations class method creates a result set and uses a println to display each row in the database, how would i get this to output form the servlet?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tom davies wrote:Also what is the best way to connect the servlet with the database?


Not to. Database access should be independent of the UI, meaning both the view and control layers. It should be ensconced completely behind the model layer.

It'd also be best to use container-managed connection pooling which the model can obtain via JNDI, helping to keep the model independent of the UI.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say for demonstration purposes, and to achieve a simple working application. Could i achieve it using what i mentioned in my first post? I dont want to over complicate things for now.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many ways, doing the DB access in the control layer, automatically complicate things. When the controllers only have to worry about control, rather than (all too often, usurping business and model logic), they become almost trivial.

If you want to continue the way you are going, I'd just follow high-level guidelines: do what increases clarity, don't do any logic in the JSPs, and so on. I don't have specific recommendations except for trying to adhere to MVC as much as possible even if you're only doing VC. (You're sort of asking "what's the best practice way to apply this poor practice?")

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the one servlet to handle multiple operations is fine as long as
- the operations are trivial
- the operations are logically related.

In this case for CRUD operations on your business object, they are definitely logically related.
You say you created a seperate class which invoked the database, so your servlet would pretty much be just gather request parameters, and invoke that Data access class.
With what you have described here, I see no problem with keeping it as one servlet.

> If it is then i encounter a problem when trying to display the results. My operations class method creates a result set and uses a println to display each row in the database, how would i get this to output form the servlet?

The standard approach would be to have your operations class method return a List of objects being the result (rather than println)
You then set that list as a request attribute, to make it available to your JSP.
Your JSP then iterates over the list of objects to display them.

That way you seperate your display logic (the JSP) from where the data is coming from (the database).

 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic