my dog learned polymorphism
The moose likes Servlets and the fly likes Servlet program with JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet program with JDBC" Watch "Servlet program with JDBC" New topic

Servlet program with JDBC

ankita balaji

Joined: Aug 19, 2004
Posts: 11
Hai Friends,
I have been working on a login servlet program.My code looks like:
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class Simple extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();


String title = "Welcome to MailServer";
out.println("<title>" + title + "</title>");

out.println("Class done<br>");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "");
out.println("Connection done<br>");

Statement st = con.createStatement();
st.executeUpdate("insert into t_holiday(yr,country) values('"+request.getParameter("yr")+"','"+request.getParameter("country")+"')");
catch(Exception e)
out.println("Exception Has Been Caught <br>" + e);

I got everything correct but when i want to see the contents of my database table after updation,its displaying null values.Iam using Mysql as my backend.I have done servlet mapping in my web.xml file.Do I need to do any other corrections?My web.xml file looks as:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"


<servlet-name> Simple </servlet-name>
<servlet-class> Simple </servlet-class>

I need help fromm you friends.
Thank You
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
try this
st.executeUpdate("insert into t_holiday(yr,country) values('"+getInitParameter("yr")+"','"+getInitParameter("country")+"')");


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Init parameters and request parameters are different. Don't get them confused. You set init parameters in the servlet configuration (web.xml). Request parameters come in via HTTP (e.g. GET or POST).

In Praful's example you would need to use something like getServletConfig().getInitParameter(param).

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Ankita, one other suggestion. Move the JDBC code out of the Servlet and into a plain old Java object for reuse. Servlets should just have code that handles the request and returns a reply. All other work should be handed off to plain old Java objects, and return the values you need to package it up into the Response.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Servlet program with JDBC
jQuery in Action, 3rd edition