this is count record code
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Count data from database in jsp</title>
</head>
<body>
<table border="0" width="75%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<h2><font color="#FF0033">Count data from database in jsp</font></h2>
<form method="POST" >
<table border="1" width="75%" cellspacing="0" cellpadding="0" bgcolor="#CCFFCC">
<%
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
int rows = 0;
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"amar","amar123");
try{
Statement st = con.createStatement();
String query = "select count(*) from user_Register";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
rows =rs.getInt(1);
%>
<%}
out.println("Total row is = "+rows);
rs.close();
con.close();
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
The given code count the number of rows in a database table.
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Count data from database in jsp</title>
</head>
<body>
<table border="0" width="75%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<h2><font color="#FF0033">Count data from database in jsp</font></h2>
<form method="POST" >
<table border="1" width="75%" cellspacing="0" cellpadding="0" bgcolor="#CCFFCC">
<%
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
int rows = 0;
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"amar","amar123");
try{
Statement st = con.createStatement();
String query = "select count(*) from user_Register";
ResultSet rs = st.executeQuery(query);
if(rs.next()) {
rows =rs.getInt(1);
%>
<%}
out.println("Total row is = "+rows);
rs.close();
con.close();
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>