Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet to connect with MySQL database

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64971
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?")

 
Stefan Evans
Bartender
Posts: 1751
10
  • Mark post as helpful
  • send pies
  • 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).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic