This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Servlet to connect with MySQL database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to connect with MySQL database" Watch "Servlet to connect with MySQL database" New topic
Author

Servlet to connect with MySQL database

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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

Joined: Jul 06, 2005
Posts: 1018
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).

 
 
subject: Servlet to connect with MySQL database