TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

JDBC

Display data from Oracle Database table using JDBC

Overview

In this tutorial, we will learn to retrieve and display data from an Oracle Database table using JDBC API in Java programming language.

Steps

There are several configuration steps that need to be done for the JDBC program to work.

  • JDK should be installed
  • Java Code editor – Eclipse IDE/IntelliJ/NetBeans is required for seamless Java development. The tutorial uses Eclipse IDE.
  • Oracle Database
  • SCOTT schema setup – The tutorial uses dept Oracle database table from the starter SCOTT schema.
  • JDBC Driver Configuration in the IDE.

JDK Install

https://www.testingdocs.com/download-and-installing-java-jdk/

Eclipse Install

https://www.testingdocs.com/install-eclipse-ide-on-windows-11/

Oracle Install

https://www.testingdocs.com/install-oracle-19c-database-on-windows-10/

Scott Schema Setup

https://www.testingdocs.com/setting-up-sample-schema-in-oracle/

JDBC Driver Setup

https://www.testingdocs.com/configure-jdbc-driver-in-java-project/

Java JDBC Program

Sample JDBC program that connects to an Oracle database and retrieves all the rows in SCOTT.dept table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/*********************************************
 * Display Oracle Database Table
 * Java Tutorials - www.TestingDocs.com
 * 
 *********************************************/

public class DisplayOracleTable {
 public static void main (String args[]) {
 //Connection descriptor variables
 String driverName = "oracle.jdbc.driver.OracleDriver";
 String hostName = "localhost";
 String portNumber = "1521";
 String serviceName = "orclpdb.localdomain";
 String username="scott";
 String password="tiger";
 String url = "jdbc:oracle:thin:@//" + hostName + ":" + 
 portNumber + "/" + serviceName ;
 //Oracle SQL SELECT query
 String sqlSelectQuery = "SELECT * FROM dept";
 Connection connection =null;
 try {
 // Load the Oracle JDBC Thin driver
 Class.forName(driverName);
 
 // Connection to the Oracle Database
 connection = DriverManager.getConnection(url, username, password); 
 
 Statement stmt = connection.createStatement();
 ResultSet rs = stmt.executeQuery(sqlSelectQuery);
 //Print Table Header
 System.out.println(String.format("%-20s", "DEPTNO")
 + String.format("%-20s", "DNAME")
 + String.format("%-20s", "LOC"));
 //Print Database Table rows in while Loop
 while (rs.next()) {
 int deptno = rs.getInt(1); 
 String dname= rs.getString(2);
 String loc = rs.getString(3);
 System.out.println(String.format("%-20s", deptno) 
 + String.format("%-20s", dname) 
 + String.format("%-20s", loc));
 }
 //Close Database Connection
 stmt.close();
 } catch (Exception exp) {
 System.out.println("Exception : "
 +exp.getMessage());
 }
 }
}

Screenshot

Display Oracle Database Table JDBC

Sample Output

Run the program from the IDE. Run as ->> Java Application

Sample program output.

DEPTNO DNAME LOC 
1000 ACCOUNTING HOUSTON 
2000 RESEARCH DALLAS 
3000 SALES CHICAGO 
4000 OPERATIONS BOSTON 

That’s it. We have successfully connected to the database, executed SELECT SQL query, retrieved the table and displayed the table rows on the console output window.

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

More information about Oracle Database:

https://www.oracle.com/database/

Related Posts

tnsnames File Oracle TestingDocs

JDBC /

Connect to Oracle Database using JDBC Thin Driver

JDBC API TestingDocs

JDBC /

JDBC API Components

JDBC /

JDBC ResultSet Interface

Configure Build Path Eclipse

JDBC /

Configure JDBC Driver in Java Project

JDBC DriverManager Class

JDBC /

JDBC DriverManager Class

‹ JDBC API Components› Connect to Oracle Database using JDBC Thin Driver

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com