aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet program with JDBC" Watch "Servlet program with JDBC" New topic
Author

Servlet program with JDBC

ankita balaji
Greenhorn

Joined: Aug 19, 2004
Posts: 11
Hai Friends,
I have been working on a login servlet program.My code looks like:
import java.io.*;
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
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");

String title = "Welcome to MailServer";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();

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")+"')");
st.close();
con.close();
}
catch(Exception e)
{
out.println("Exception Has Been Caught <br>" + e);
}
out.println("</body>");
out.println("</html>");
}
}

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"
"http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>

<servlet>
<servlet-name> Simple </servlet-name>
<servlet-class> Simple </servlet-class>
<init-param>
<param-name>yr</param-name>
<param-value>1998</param-value>
</init-param>
<init-param>
<param-name>country</param-name>
<param-value>india</param-value>
</init-param>



</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/Simple</url-pattern>
</servlet-mapping>
</web-app>
I need help fromm you friends.
Thank You
Ankita
Praful Thakare
Ranch Hand

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

Cheers
-Praful


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).

Jules
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet program with JDBC