JDBC Select Program Example

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.

JDBC Select on executeQuery() :
  1. ◈ We can call the executeQuery() method on statement object to select the data from the database.
  2. ◈ executeQuery() method returns ResultSet object.
  3. ◈ The ResultSet object contains rows of table.
  4. ◈ The ResultSet object maintains the cursor, and it is initially positioned at before of the first row.
  5. ◈ 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.
  6. ◈ The next() method returns a boolean value, either true or false.
  7. ◈ When there is a row available in ResultSet object then next() returns true if no more rows then it returns false.
  8. ◈ By default ResultSet object is non-scrollable. That means, a cursor of the ResultSet object can be moved in forward direction only.
  9. ◈ 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.
  10. ◈ 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.

JDBC Select Program Example :

                        package com.navabitsolutions.jdbc; 
 
                        import java.sql.Connection; 
                        import java.sql.DriverManager; 
                        import java.sql.ResultSet; 
                        import java.sql.Statement; 
                         
                        public class JdbcSelectExample { 
                         
                            public static void main(String[] args) throws Exception { 
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
                                Connection con = DriverManager.getConnection( 
                                        "jdbc:mysql://localhost:3306/navabitsolutions", "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(); 
                            } 
                         
                        }