Write a java program to count the number of records in a table
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Count_Example {
public static void main(String args[]) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement object
Statement stmt = con.createStatement();
//Query to get the number of rows in a table
String query = "select count(*) from Cricketers_Data";
//Executing the query
ResultSet rs = stmt.executeQuery(query);
//Retrieving the result
rs.next();
int count = rs.getInt(1);
System.out.println("Number of records in the cricketers_data table: "+count);
}
}
0 Comments