Sunday, December 12, 2010

EmployeeServlet Program.................

package com.nareshit.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class EmployeeServlet extends HttpServlet{
Connection con;
PreparedStatement ps;
public void init(ServletConfig config) throws ServletException{
String driver=config.getInitParameter("driver");
String cs=config.getIntitParameter("url");
String usr=config.getInitParameter("user");
String pwd=config.getInitParameter("pwd");
 try
  {
  Class.forName(driver);
  con=DriverManager.getConnection(cs,usr,pwd);
  ps=con.PrepareStatement("Insert into employee values(?,?,?)");
  }
 catch(ClassNotFoundException e){
 e.printStackTrace();
 }
 catch(SQLException e)
 {
 e.PrintStackTrace();
 }
}//init()
public void destroy(){
try
  {
   if(ps!=null)
     ps.close();
   if(con!=null)
     con.close();
  }
catch(SQLException e)
  {
  e.PrintStackTrace();
  }
  }
public void doPost(HttpServletRequest request,HttpServletResponse response)
 throws ServletException ,IOException
 {
 String a=request.getParameter("empno");
 String b=request.getParameter("name");
 String c=request.getParameter("salary");
 int eno=Integer.parseInt(a);
 float salary=Float.parseFloat(c);
 try
 {
 ps.setInt(1,eno);
 ps.setString(2,name);
 ps.setFloat(3,salary);
 ps.executeUpdate();
 }
 catch(SQLException e)
 {
  e.PrintStackTrace();
 }
 response.sendRedirect("emp.html");
 }//doPost()
}//EmployyeeServlet class

No comments:

Post a Comment