File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes querying a database within a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "querying a database within a servlet" Watch "querying a database within a servlet" New topic

querying a database within a servlet

April Benzine

Joined: Sep 23, 2004
Posts: 17
Hi all,
I am trying to execute a query in a servlet. I have a connection.

--a static html page--
The user clicks on a radio button for one of the following this is all in a form and the form works)

Then there is a text box to enter in criteria(This works too)
Then they click submit and it takes them to my page (this works)
In my init method I have my employeeSearch object instantiated and loaded.
(this makes a connection and does a test query)- this works

???Now I have to run a query on the database. How do I do this?
I know this goes in my doGet method. Please help me.
Thank You
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
No what you know is not right, sorry. Ofcourse you can so that but you should not. It is strongly discouraged.

Please make your servlet and your jdbc code seperate. Servlet should be just to entertain the requests and giving responses. You can do your jdbc stuff in simple, normal java class.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

Have you seen the JDBC tutorial from Sun?

It walks you through how to get started with JDBC. And Adeel is correct that it is best not to put the JDBC in the servlet. The servlet would instead call your database class.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: querying a database within a servlet
It's not a secret anymore!