Jdbc Select Program Example | executeQuery() Example in JDBC

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to understand the basic JDBC select program example. As we already discussed in the previous tutorials steps to create jdbc example, to do the select operation in JDBC we need to use the executeQuery() method.
While Executing the executeQuery() method, we need to keep in mind some important facts.
We can call the executeQuery() method on statement object to select the data from the database.
executeQuery() method returns ResultSet object.
The ResultSet object contains rows of table.
The ResultSet object maintains the cursor, and it is initially positioned at before of the first row.
To read the data of row by row, we need to move the curson to the next row by calling next() method available in ResultSet.
The next() method returns a boolean value, either true or false.
When there is a row available in ResultSet object then next() returns true if no more rows then it returns false.
By default ResultSet object is non-scrollable. That means, a cursor of the ResultSet object can be moved in forward direction only.
If require, we can convert a ResultSet object into Scrollable type. If the ResultSet object is scrollable then the cursor will be moved in forward and backward direction.
One statement object can create only one ResultSet object at a time. If another ResultSet object is created by same statement object, then it automatically closes the previous ResultSet object.
ResultSet rs1 = stmt.executeQuery("select * from emp);
ResultSet rs2 = stmt.executeQuery("select * from dept);
 
Here rs1 is closed automatically, when rs2 is created.
package com.onlinetutorialspoint.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
 
public class Jdbc_Select_Example { 
 
    public static void main(String[] args) throws Exception { 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        Connection con = DriverManager.getConnection( 
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", "123456"); 
        Statement stmt = con.createStatement(); 
        ResultSet rs = stmt.executeQuery("select * from student"); 
        while (rs.next()) { 
            System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " 
                    + rs.getString(3)); 
        } 
        rs.close(); 
        stmt.close(); 
        con.close(); 
    } 
 
} 
Output :
101 Chandra hyd
102 Shekhar hyd
Happy Learning 🙂

JDBC Interview Questions and Answers
Hibernate Query Language (HQL) Select Operation
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file

JDBC Interview Questions and Answers
Hibernate Query Language (HQL) Select Operation
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions