Site icon TestingDocs.com

Connect to Oracle Database using JDBC Thin Driver

Overview

In this tutorial, we will learn the steps to connect to Oracle Database using JDBC Thin Driver in Java programming language. In this example, we will connect to SCOTT schema on a pluggable database named orclpdb.

Checklist

Install the database, if you do not already have an Oracle Database installed. Sample steps to install Oracle on Windows operating system:

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

For Detailed Steps follow the vendor’s instructions for the database installation.

Instructions to setup the sample SCOTT schema can be found here:

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

Oracle JDBC Thin Driver

The JDBC driver can be found at the following location on the database machine:

%ORACLE_HOME%\jdbc\lib

For standalone Java project add the JDBC jar file to the project build path.

Connection Descriptor

The connection descriptor string should be formed using the tnsnames.ora file from the database instance that we are trying to establish the connection. The exact hostname, port, and the service name can be found in the tnsnames.ora file.

On Windows the file can be found at the following path: %ORACLE_HOME%\network\admin

Java Sample Program

 

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

/*********************************************
 * Oracle JDBC Thin Driver Demo
 * Print JDBC Version Information
 * Java Tutorials - www.TestingDocs.com
 * 
 * @author testingdocs
 *
 *********************************************/
public class OracleThinJDBCVersion
{
	public static void main (String args[])
			throws SQLException
	{
		OracleDataSource ods = new OracleDataSource();
		ods.setURL("jdbc:oracle:thin:scott/tiger@
//localhost:1521/orclpdb.localdomain");
		Connection connection = ods.getConnection();
		// Create Oracle DatabaseMetaData Object
		DatabaseMetaData meta = connection
.getMetaData();
		// Display JDBC Driver Version Info
		System.out.println("Oracle JDBC Thin Driver
 Version: " + meta.getDriverVersion());
	}
}

Screenshot

The program connects to the Oracle database and prints the JDBC version on to the console 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/

Exit mobile version