Find Number of Rows using Servlet

Find Number of Rows using Servlet

Posted in : Java Posted on : July 8, 2011 at 6:47 PM Comments : [ 0 ]

In this section we will discuss how can you find the number of rows in a table using servlet.

Find Number of Rows using Servlet

In this section we will discuss how can you find the number of rows in a table using servlet.

To solve this problem first we would have to make a table in database. So I created a table 'data' in MS Access. Further I created a java file  which extends the HttpServlet. In doGet() method use the getWriter() method of ServletRespsonse interface which is further extended by the HttpSerevletResponse that helps in to write on browser. Further in the try-catch block first I load the driver and make a connection to the database and then write the query "insert" for inserting data into the table and "select count(*)" query for counting the number of rows in a table. In this example I am inserting and counting the number of rows simultaneously. In final step I close the Connection, PreparedStatement, and ResultSet. I also created a html page from which getParameter() method of HttpServletRequest gets the parameter request and stored it into the table in their respective columns.

Example :


<form method="get" action="/columnRow/test">
<table style="width: 100%">
<input name="name" type="text" />
<input name="age" type="text" />
<input name="pass" type="password" />
<input name="Submit" type="submit" value="submit" />

import java.sql.Connection;
import java.sql.DriverManager;
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 GetRowNumber extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
PrintWriter pw = res.getWriter();
String nm = req.getParameter("name");
Integer age = Integer.parseInt(req.getParameter("age"));
String psw = req.getParameter("pass");
Connection con;
PreparedStatement ps;
ResultSet rs;
try {
con = DriverManager.getConnection("jdbc:odbc:test");
ps = con.prepareStatement("insert into data values (?,?,?)");
ps.setString(1, nm);
ps.setInt(2, age);
ps.setString(3, psw);
int i = ps.executeUpdate();
if (i != 0)
pw.println("Your data has been added successfully.<br>");
ps = con.prepareStatement("select count (*) from data");
rs = ps.executeQuery();
while ( 
pw.println("And now the total number of rows (record) =<b> " + rs.getInt(1)+"</b>");
catch (SQLException e) {
catch(ClassNotFoundException ex)


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

Output :

When you will execute the above example you will get the following output :

As I adding the records into the table so, after adding the record into the table the following message is displayed.

And the records after updating is

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

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

Tutorial Topics