JDBC – Display All Records

Access all records from table: (static query)

  • executeQuery() method is used to execute DRL command
  • executeQuery() method returns ResultSet object contains all records of that table.
  • We process the data using ResultSet Object methods.
package online;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AccessData
{
            public static void main(String args[]) throws Exception
            {
                        Connection conn = null;
                        String driver = “oracle.jdbc.driver.OracleDriver”;
                        try
                        {
                                    Class.forName(driver);
                                    System.out.println(“Driver loaded”);
                                    String url = “jdbc:oracle:thin:@localhost:1521:xe”;
                                    String uname = “system”;
                                    String pwd = “admin”;
                                    conn = DriverManager.getConnection(url, uname, pwd);
                                    System.out.println(“Connection is ready”);     
                                   
                                    Statement stmt = conn.createStatement();
                                    String query = “select * from account”;
                                    ResultSet rs = stmt.executeQuery(query);       
                                    System.out.println(“Account table details are : “);
                                    while(rs.next())
                                    {
                                                System.out.println(rs.getInt(1) + ” , ” + rs.getString(2) + ” , ” + rs.getInt(3));
                                    }
                        }
                        finally
                        {
                                    if(conn != null)
                                    {
                                                conn.close();
                                                System.out.println(“Connection closed”);
                                    }
                        }
            }
}

2 thoughts on “JDBC – Display All Records”

  1. Very nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed browsing your blog posts.
    After all I will be subscribing to your feed and I hope
    you write again soon!

  2. It’s the best time to make a few plans for the long run and it’s time to be happy.
    I’ve learn this put up and if I may I desire to recommend you some fascinating issues or tips.
    Perhaps you could write next articles referring to this article.
    I want to learn more issues about it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top