Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

JDBC Execute Query

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


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

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. ( ) -  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.

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 {
  con = DriverManager.getConnection(url + db, user, pass);
  st = con.createStatement();

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

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


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

Download code


Share on Google+Share on Google+

JDBC Execute Query

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JDBC Execute Query   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image