This article will show you example about how to connect Oracle DB use JDBC driver.
1. Install Oracle DB.
- Go to oracle db download page to get the latest version if you do not have one installed.
- Install Oracle DB follow installation page instructions.
2. Get Oracle JDBC Driver jar.
- Go to Oracle jdbc driver page and get suitable version.
- You can download the jar file directly.
- 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>
- The easiest way to find the ojdbc jar is go to http://search.maven.org and search ojdbc in the search box.
- After download, add the ojdbc jar file into your 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:
- [download id=”2511″]