• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet program with JDBC

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this
st.executeUpdate("insert into t_holiday(yr,country) values('"+getInitParameter("yr")+"','"+getInitParameter("country")+"')");

Cheers
-Praful
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic