Connect JDBC Application with Oracle Database

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

  1. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    98
    Likes Received:
    18
    Trophy Points:
    8
    Location:
    India
    Home Page:
    httpS://persys.in/
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice