JDBC Execute Query

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

Ads

Tutorials   
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
Ads

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

Ads
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

Advertisements

Ads
 

Discuss: JDBC Execute Query   View All Comments

Post your Comment


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

Ads

 

Ads