How To Connect Oracle DB Use JDBC

This article will show you examples of how to connect Oracle DB use the JDBC driver.

1. Install Oracle DB.

  1. Go to the 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 the Oracle JDBC driver page and get a suitable JDBC driver jar version.
  2. You can download the jar file directly.
  3. You can also get the JDBC jar via the maven repository. You can refer to this blog post. Add the below dependency in your java project pom.xml file.
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>19.3.0.0</version>
    </dependency>
  4. The easiest way to find the ojdbc jar file is to go to http://search.maven.org and search the keyword ojdbc in the search box.
  5. After download, add the ojdbc jar file into your Eclipse java project Java Build Path. You can refer to the article How To Add Selenium Server Standalone Jar File Into Eclipse Java Project And Maven Project.

 3. Oracle JDBC Connection Java Example Code.

  1. Please note the JDBC connection url format is : jdbc:oracle:thin:@db_ip:dp_port:db_instance_id, below is an example of how to connect to the Oracle database use JDBC in java source code.
     /* 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 Source Code.

  1. Below is the full source code of this example. It shows how to get a connection to the Oracle database using JDBC in java.
    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 oracle 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;
    		}
    	}
    
    }
    
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x