1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Connect JDBC Application with Oracle Database

Discussion in 'Java' started by persysweb, Feb 25, 2018.

  1. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    90
    Likes Received:
    13
    Trophy Points:
    8
    Location:
    India
    Home Page:
    To connect Java Application with Oracle database :

    To connect Java Application with Oracle 10g you need to follow following steps :

    Create the database for eg. Create the database of the employee as tablename emp.

    create table emp(id number(10),name varchar2(40),age number(3));

    1. Load the driver class : Driver class for the oracle database is :
    oracle.jdbc.driver.OracleDriver.

    2. Create the Connection object :The connection URL for the oracle10G database is jdbc:eek:racle:thin:mad:localhost:1521:xe
    where jdbc is the API , Oracle is the database , thin is the driver, localhost is the server name, 1521 is the port number and XE is the oracle service name.

    3. Create the statement
    Statement stm=con.createStatement();

    4. Execute the query
    ResultSet rs=stm.executeQuery("select * from emp);

    5. Close the Connection
    con.close();

    In following example admin is the username and admin1 is the password of the Oracle Database.
    Example :
    Code:
    import java.sql.*;
    class Test{
    public static void main(String args[]){
    try{
    //step1 load the driver class
    Class.forName("oracle.jdbc.driver.OracleDriver");
     
    //step2 create  the connection object
    Connection con=DriverManager.getConnection(
    "jdbc:eek:racle:thin:mad:localhost:1521:xe","admin","admin1");
     
    //step3 create the statement object
    Statement stmt=con.createStatement();
     
    //step4 execute query
    ResultSet rs=stmt.executeQuery("select * from emp");
    while(rs.next())
    System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
     
    //step5 close the connection object
    con.close();
     
    }catch(Exception e){ System.out.println(e);}
     
    }
    }
     

Share This Page