CONNECT ORACLE DATABASE FROM JAVA

If you have any queries please leave a message here
Your Message
×


  1. Install Windows Operating System

  2. Install Oracle

    If you want to access oracle database from another computer, see Oracle Configuration

  3. Install Java and Set Environment Variables

  4. Copy ojdbc14.jar file into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) and then extract here.

    If you do not keep your *.java files into the bin folder of JDK (C:\Program Files\Java\jdk1.8.0_112\bin) then you must need to extract the ojdbc14.jar file into the location where your *.java files are present.

Driver Class The driver class for the oracle database is oracle.jdbc.driver.OracleDriver
Connection URL

The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe

  • jdbc is the API

  • oracle is the database

  • thin is the driver

  • localhost is the server name on which Oracle is running, we may also use IP address

  • 1521 is the port number

  • XE is the Oracle service name

You may get all these information from the tnsnames.ora file
Username The default username for the oracle database is system
Password Password is given by the user at the time of installing the oracle database



Example
Write a program to create employee table and insert 3 records into it

//CreateEmployeeTable.java
import java.sql.*;
import oracle.jdbc.driver.*;
public class CreateEmployeeTable
{
public static void main(String args[])
{
try
{
// Oracle Database Connection
String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="system";
String password="12345";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(url,user,password);

// Query Execution
Statement stmt=con.createStatement();
stmt.executeUpdate("create table employee(empid varchar(10) primary key,empname varchar(50),salary number(8,2))");
stmt.executeUpdate("insert into employee values('E-1010','Mr. X',25000)");
stmt.executeUpdate("insert into employee values('E-1020','Mr. Y',50000)");
stmt.executeUpdate("insert into employee values('E-1030','Mr. Z',35000)");

// Close The Connection
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us