Home Jdbc JDBC Next

Related Tutorials


 
 

Share on Google+Share on Google+

JDBC Next

Advertisement
In this Tutorial we help you to understand an example from JDBC Next.

JDBC Next

     

The Tutorial help in understanding JDBC Next. The program describe a code that gives you the number of rows in the table. The code include a class JDBC Next, Inside the class we have a main method that follows the list of steps to get the number of rows in a table -

  Import a package java.sql.* : This package provides you set of all classes that enables a network interface between the front end and back end database. Loading a driver is the next step to be followed by calling a class.forName( ),this accept a driver class as argument.

 DriverManager.getConnection( ) : The Driver Manager call a get Connection method ,which return a connection object, this object built a connection between url-backend.

 con.createStatement ( ) : This is used to create sql object. An object of connection class is used to send and execute the sql queries in database.

  execute Query( ) : This is used to retrieve the record set from the database and the retrieve record set is assigned in Result Set. The select statement is used to retrieve the record set from table.

Finally the rs.next ( ) is used to retrieve the next sequence element in the series.

The println print the number of row as output.

JdbcNext.java


import java.sql.*;

public class JdbcNext {

  static public final String driver = "com.mysql.jdbc.Driver";
  static public final String connection =
  "jdbc:mysql://localhost:3306/komal";
  static public final String user = "root";
  static public final String password = "root";

  public static void main(String args[]) {

  try {
  Class.forName(driver);
  Connection con =
 DriverManager.getConnection(connection, 
 user, password);

  Statement st = con.createStatement();

  String sql = "select * from person";
  ResultSet rs = st.executeQuery(sql);
  
  int i=0;
  while(rs.next()){
  i++;
  }
  System.out.println("No. of Row :"+i);
  
  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

No. of Row :2

Download code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 18, 2008

Related Tutorials

Discuss: JDBC Next  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com