This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem in getting the data from DB using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in getting the data from DB using servlets" Watch "Problem in getting the data from DB using servlets" New topic

Problem in getting the data from DB using servlets

Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
I am developing one small application using servlets/JSP in tomcat server. Below is the sample coding I am using. I have used one class called connectionpool which will give us the connection. I am using that class to get the connection in servlet and then I am passing the connection to the helper class where all the database operations like insert,delete,update and select queries are done. I am passing the connection only and I am creating the statement and resultset in the helper itself and closing it there itself after getting & storing the required data from db. I am finally closing the connection in the servlet itself.

Sometimes the application is getting hanged and nothing is happening. I guess there is some connection pooling/performance problems are there,. I have the sample coding.

Is it right way of doing it? Any to use Apache Connection Pooling? any sample coding available on using.

Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
It's a lot of code. If you want good help I advise that you specify your question better.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
Simplest form given. Sometimes, when I try to retrieve records the application gets hanged. no error message is displying in tomcat console... that'y I am asking if there is any flaw in my coding. for exmple I am trying to retieve the emloyee details from db. employee table has only limited records of 10-15 records only and sometimes even for this it's retieving and application gets hanged and if I deleting some records then it's working. Since It's not displaying any error message..I have doubts whether any flaw in the logic...

Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Mike, honestly I didn't get where the problem is, but i want you to try something which might be the reason. (i said might be)..
your connection is being created over and over, which once I've read in an article that said it can't be, you should create it once, and close it once..
so it's better you try creating a Servlet implement of ServletContextListener, this servlet will run once your application is loaded, and it creates the connection, and assign it as a context scope attribute, check out the code:

This is the best way to have your connection "handy" by all servlets.
try it, it might be the problem.

for more understanding:
- ServletContextListener doc

Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
thanks for your reply. I will try this and post you the reply..thanks a lot.. I have seen in my code whenever I am clicking a link in the page it will go to the servlet and first it will create one connection...and i saw sometimes more than one connection is will try it out & will post the reply
Mike Thomson
Ranch Hand

Joined: Nov 07, 2007
Posts: 115
I have implemented your coding for getting the connection in the servlet context and it didn't and I have faced the same problem of keep on loading.. after a while, I have removed the "ORDER BY" in the query and to my surprise it's worjing properly and it's not keep on loading now. I think I haven't used the order by properly in sql query. Any body knows why using order by causing issues like this?
It is sorta covered in the JavaRanch Style Guide.
subject: Problem in getting the data from DB using servlets
Similar Threads
Cannot display data from database
retrieving data from a databse into an excelsheet
Problem in displaying multiple search results
Data being retrieved even though MySQL driver is not loaded
Problem in retrieving data and store it in string