Skip to main content

Java Program to connect Oracle Database using JDBC

Steps to connect java Application with Oracle database

1.In order to connect java app with oracle database, Oracle driver is needed.
First Download Oracle Driver class using Below link.
Oracle JDBC Driver

jdbc-driver-for-oracle
2. Right click on java Project and select Build path and Select Add external jar

3. Add Oracle Driver jar file.

4. Create employee_t table into Oracle database.

  CREATE TABLE EMPLOYEE_T 
  (
  ID NUMBER , NAME VARCHAR2(20) , SAL NUMBER , DEPTNO NUMBER 
  ) 
  

Java Program to Connect with Oracle DB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
 
public class OraDB 
{
	public static void main(String[] args) 
	{
 
 
        try
 
           {
 
            //Load the driver class
             Class.forName("oracle.jdbc.driver.OracleDriver");
 
             //2nd way to load the driver	
             //DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
 
            //getting connection via driver
             Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "hr","hr");
 
 
 
            //issue Sql query
 
            Statement st = con.createStatement();
 
            //query to insert data into database
            String query="insert into hr.employee_t values(101,'bhavesh',20,000,21)";
 
            int c=st.executeUpdate(query);
 
            System.out.println("Row affected: "+c);
 
            } 
 
           catch (Exception e)
 
            {            
            e.printStackTrace();
            }
 
       }		
 
}

Output: Row affected: 1

See the inserted data in Oracle DB
oraccle-db-jdbc