Servlet Code to Delete information from SQL database Tomcat server

In this post we will illustrate how to delete information from database.
This post is continued from previous post have a look on how to do:
1. Database settings and insetion of data into SQL database.
2. Searching information from database.

Create a html form delinfo.html

<html>
<head>
<h1><u>student form</u></h1>
</head>
<body bgcolor="DarkSalmon">
<form name=Student method=POST action=http://localhost:8080/examples/servlet/delinfo>

<font face="Copperplate Gothic Bold">ID</font face>:<input type="text" name="t2" size="30"><p><br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>


Now Create a delinfo.java which has the actual logic for deleting information from database.

import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class delinfo extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
try
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String s2=req.getParameter("t2");
out.println("<html><body><h1>............................servlet deletion working..........:):).......</h1></body></html>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
out.println("<html><body><h4>connectiopn established........</h4></body></html>");
if(con!=null)
{
out.println("<html><body><h4>connection established........</h4></body></html>");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("delete from studentinfo where id1='"+s2+"'");
out.println("<html><body><h4>deleted from database.......</h4></body></html>");
stmt.close();
}



con.close();
out.close();


}
catch(Exception e)
{}
}
}

Deleting information from SQL database
Deleting information from SQL database




0 comments:

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link