Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC Fetch

In this Tutorial we want to describe you an example from Jdbc Fetch.

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 Fetch

     

The Tutorial describe you a code that fetch the elements from a database. In this Tutorial we want to describe you an example from Jdbc Fetch. In this program code, the System.println print the fetch data from database. The code illustrates a class Jdbc Fetch, Inside the main method we have the list of steps to be followed - 

The first step is to import a java.sql.* ,this provides you a network interface helps you in communicating between the front end Java application and the backend Loading a driver  follows the next step, by calling a class.forName ( ) with driver class passed as argument.

   DriverManger.getConnection( ) :This method return you a connection object, This method is used to built a connection between the url and database. The createStatement ( ) return you a statement object, this statement is used to execute and send SQL query in the database.

The Statement object call execute Query ( ),The method is used to fetch  the record set from employee table, that accept sql passed as an argument.

   Finally on execution the println print the element from database using getString ( )  from the record set.
  getString( ):The method return and retrieves the value of the specified column name in the current row of this  object as a String in the Java programming language.

 

JdbcFetch.java

import java.sql.*;



public class JdbcFetch {



  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 employees";

  rs = st.executeQuery(sql);



  while (rs.next()) {

  System.out.print(rs.getString(1) + " \t");

  System.out.print(rs.getString(2) + "  \t");

  System.out.print(rs.getString(3)+ " \t");

  System.out.println(rs.getString(4) );

  }

  catch (Exception e) {

  System.out.println(e);

  }

  }

}

Output

Girish     	Tewari      	Rohini       	Indian
Komal     	Singh      	Rohini       	Indian
Sandeep     	kumar      	Rohini       	Indian
Amit     	Singh      	Rohini       	Indian
Girish     	Tewari      	Rohini       	Indian
Darshan     	Tewari      	Rohini       	Indian

Download code

Advertisements

Share on Google+Share on Google+

JDBC Fetch

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 Fetch   View All Comments

Post your Comment


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

Ads

 

Ads