Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

Java Servlet :Inserting data from database table
Previous Home Next

This example shows you how to insert the data in the database table by using the JDBC and the servlet.

index.html

<html>
	<head>
	 <title>Inser data from table</title>
	</head>
	<body>
	 <form method="get" action="loginServlet">
	 Name<input type="text" name="txtName"><br>
	 Password<input type="password" name="password"><br>
	 <input type="submit" value="submit">
	 </form>
	</body>
</html>

web.xml

<web-app>
	<servlet>
	 <servlet-name>s1</servlet-name>
	 <servlet-class>RegisterServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	 <servlet-name>s1</servlet-name>
	 <url-pattern>loginServlet</url-pattern>
	</servlet-mapping>
</web-app>

 

InsertServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class InsertServlet extends HttpServlet
{
 public void doGet(HttpServletRequest request,
  HttpServletResponse response)throws ServletException,IOException
{
  response.setContentType("text/html");
  PrintWriter out=response.getWriter();
  String name=request.getParameter("txtName");
  String pass=request.getParameter("password");
  try
  {
	Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection
		("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");	
    PreparedStatement stmt=con.prepareStatement
		("insert into emp values(?,?)");
    stmt.setString(1, name);
    stmt.setString(2, pass);
    int i=stmt.executeUpdate();
    if(i!=0)
    out.println("<b>You are successfully Registered</b>");
    con.close();
   }
   catch(Exception e)
   {
    out.println("<b>Registration failed</b>");
    out.println("<b>Error:</b>" +e);
   }
 }
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: