Delete row using servlet

Delete row using servlet

Posted in : Java Posted on : July 8, 2011 at 7:12 PM Comments : [ 0 ]

In this section we will discuss how can we delete a single row or specified row.

Delete row using servlet

In this section we will discuss how can we delete a single row or specified row.

To solve this problem we will have to require a database table so, I first created a database table named 'data' and enter some records into it. Then I make a class named DeleteRow in java. This class extends the HttpServlet class further implemented the method doGet(). Inside the doGet() method uses the getWriter() method of ServletResponse interface with the reference of HttpServletResponse interface. Then establish a connection of database in java code and then deleted a specified row as in our example uses "delete from data where name = 'bipul' ".

Example :

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteRow extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
PrintWriter pw = res.getWriter();
Connection con;
PreparedStatement st;
ResultSet rs;
con = DriverManager.getConnection("jdbc:odbc:test");
st= con.prepareStatement("delete from data where name='bipul'");
int i = st.executeUpdate();
pw.println("Deleting row...");
else if (i==0)
pw.println("<br>Row has been deleted successfully.");
catch(SQLException sx)
catch(ClassNotFoundException cx)


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


Output :

Initially the database was

After running the example you will get the following output (when run first time).

When you will execute the example second time

The database table after deleting the specified row.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics