Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet program with JDBC

 
ankita balaji
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
st.executeUpdate("insert into t_holiday(yr,country) values('"+getInitParameter("yr")+"','"+getInitParameter("country")+"')");

Cheers
-Praful
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic