How many records are present in database using JSP

Introduction 

 
//To know how many records are present in database using jsp
 
To see total listing of .jsp files first we have to configure the web.xml file present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) folder.Search the <param-name>listings</param-name> tag and below this tag <param-value>false</param-value>,change the value to true(<param-value>true</param-value>)
 
Create any folder in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps)and save the below program as totalrecords.jsp
  1. <%@ page import="java.sql.*" %>  
  2. <%  
  3. try   
  4. {          
  5. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  6. }        
  7. catch(ClassNotFoundException e)  
  8. {           
  9. out.println("Class not found "+ e);     
  10. }        
  11. out.println("JDBC Class found");     
  12. int no_of_rows = 0;   
  13. try   
  14. {      
  15. Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","system","pintu");  
  16. Statement stmt = con.createStatement();    
  17. ResultSet rs = stmt.executeQuery("SELECT * FROM employee");      
  18. while (rs.next())  
  19. {              
  20. no_of_rows++;     
  21. }           
  22. out.println("There are "+ no_of_rows + " record in the table");    
  23. }        
  24. catch(SQLException e){   
  25. out.println("SQL exception occured" + e);    
  26. }  
  27. %>  
Next Recommended Reading Display records from database using JDBC