try {
db = new DBConnection();
conn = db.getConnection();
pst = conn
.prepareStatement("select ename from employee_hobby where hobby=?");
pst.setString(1, request.getParameter("hobby"));
but i am getting null as value of conn every time..
I see there's a TODO in the code; I'd start by addressing that. At a minimum, print something to the log files, like "e.printStackTrace()". I bet that's where the problem is - ignoring an exception that you simply cannot ignore without consequences.
you can do it in this way
1)Declare a Connection Class
2)now either set the <loadonstartup> element an attribute of servlet in web.xml to some +ve value
The ContextListener Class will be called when context will be initialized and that the servlet will load on start up
now in either of them declare a method that will connect with the database and then set this connection object as your context attribute and get the attribute weherever required and procced with your
database operations