Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc JDBC Execute Query

Related Tutorials


 
 

Share on Google+Share on Google+

JDBC Execute Query

Advertisement
In this Tutorial we want to explain you an example from JDBC Execute query,

JDBC Execute Query

     

The  Execute Query in JDBC retrieve the elements from a database. In this Tutorial we want to describe you a code that helps you to understand JDBC Execute Query, For this we have a class JdbcExecutequery,Inside the main method, the list of steps that have to be done in order to access the elements from database are given below -Java.sql.* - This Java.sql* package is imported that contain the definition for set of all classes and this classes provides you the network connectivity between the database and url. 

  Loading a Driver : The next step is to load  a driver by calling a Class.forname( ) and accept the argument as driver class.When a driver is loaded, you connect the front end of your Java application to the backend database. 

 con.setAutoCommit(false) :  This method is used to disable the commit method.

 DriverManager.getConnection (  ) : The Driver Manager call get Connection ( ) method return a connection object. This method is used to connect database and a Url.

 con.createConnection( ) : This method provides you an  Sql object. Once a connection is built ,your application can interact with backend database. A connection object is used to send and execute SQL Statement to a backend database.

 executeQuery (sql ) -  This method is used to retrieve the element from a database by accepting sql as argument.

 rs.next ( ) -  This method is used to retrieve the successively elements in series.

The println print the value of the number, name and date of birth.In case there is an exception occurred  in loading a database driver  in the try block. The catch block check the exception and println print the exception.

JdbcExecutequery.java.java

import java.sql.*;

public class JdbcExecutequery {

  public static void main(String args[]) {

  Connection con = null;
  Statement st = null;
  ResultSet rs = null;

  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);
  st = con.createStatement();

  String sql = "select * from person";
  rs = st.executeQuery(sql);

  System.out.println("no.  \tName  \tDob");
  while (rs.next()) {
  System.out.print(rs.getString("id") + " \t");
  System.out.print(rs.getString("cname") + " \t");
  System.out.println(rs.getDate("dob"));
  }
  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

no.  	Name      	Dob
1   	Girish     	1984-06-02
2   	komal     	1984-10-27

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 17, 2008

Related Tutorials

Discuss: JDBC Execute Query   View All Comments

Post your Comment


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