How To Connect Oracle DB Use JDBC

This article will show you example about how to connect Oracle DB use JDBC driver.

1. Install Oracle DB. 

  1. Go to oracle db download page to get the latest version if you do not have one installed.
  2. Install Oracle DB follow installation page instructions.

2. Get Oracle JDBC Driver jar.

  1. Go to Oracle jdbc driver page and get suitable version.
  2. You can download the jar file directly.
    download oracle jdbc driver
  3. You can also get ojdbc jar via maven repository. You can refer to this blog post.
    <groupid>com.oracle.jdbc</groupid>
    <artifactid>ojdbc8</artifactid>
    <version>12.2.0.1</version>
  4. The easiest way to find the ojdbc jar is go to http://search.maven.org and search ojdbc in the search box.
    search oracle ojdbc driver jar file in maven repository
  5. After download, add the ojdbc jar file into your Eclipse java project.
    add oracle ojdbc jar file in eclipse java project

 3. Oracle JDBC Connection Java Example Code.

Please note the jdbc connection url format is : jdbc:oracle:thin:@db_ip:dp_port:db_instance_id

 /* Register jdbc driver class. */
 Class.forName("oracle.jdbc.driver.OracleDriver");
			
 /* Create connection url. */
 String connUrl = "jdbc:oracle:thin:@localhost:1521:DataTiger";
			
 /* user name. */
 String userName = "system";
			
 /* password. */
 String password = "manager";
			
 /* Get the Connection object. */
 java.sql.Connection conn = DriverManager.getConnection(connUrl, userName , password);

4. Complete Example Code.

public class OracleExample {

	public static void main(String[] args) {
		
		try
		{
			OracleExample oracleExample = new OracleExample();
			
			Connection conn = oracleExample.getOracleConnection();
			
			/* You can use the connection object to do any insert, delete, query or update action to the mysql server.*/
			
			/* Do not forget to close the database connection after use, this can release the database connection.*/
			conn.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	/* This method return java.sql.Connection object. */
	public Connection getOracleConnection()
	{
		/* Declare and initialize a sql Connection variable. */
		Connection ret = null;
		
		try
		{
		
			/* Register jdbc driver class. */
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			/* Create connection url. */
			String connUrl = "jdbc:oracle:thin:@localhost:1521:DataTiger";
			
			/* user name. */
			String userName = "system";
			
			/* password. */
			String password = "manager";
			
			/* Get the Connection object. */
			ret = DriverManager.getConnection(connUrl, userName , password);
			
			/* Get related meta data for this mysql server to verify db connect successfully.. */
			DatabaseMetaData dbmd = ret.getMetaData();
			
			String dbName = dbmd.getDatabaseProductName();
			
			String dbVersion = dbmd.getDatabaseProductVersion();
			
			String dbUrl = dbmd.getURL();
			
			String uName = dbmd.getUserName();
			
			String driverName = dbmd.getDriverName();
			
			System.out.println("Database Name is " + dbName);
			
			System.out.println("Database Version is " + dbVersion);
			
			System.out.println("Database Connection Url is " + dbUrl);
			
			System.out.println("Database User Name is " + uName);
			
			System.out.println("Database Driver Name is " + driverName);
						
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			return ret;
		}
	}

}

5. Source Code:

  1. Download “OracleExample.zip” OracleExample.zip – Downloaded 121 times – 925 B

(Visited 1,632 times, 7 visits today)
READ :   JDBC Create Table Example Use Statement

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.