Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Steps to connect to the Oracle Database using JDBC in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Steps to connect to the database in java using JDBC for Oracle database

    1) Driver class is registered

    The driver class is registered first with the help of the forName() method.

    e.g.

    Class.forName("oracle.jdbc.driver.OracleDriver");  
    

    2) Connection object is created

    This is used to create a connection with the database and getConnection() method is used for this purpose.

    e.g.

      Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","root","password");  
    

    3) Statement object is created

    We use the createStatement() which belongs to Connection interface for creating the statement. This object is used to execute the database queries.

    e.g.

    Statement st=con.createStatement();  
    

    4)Executing the query

    We use the executeQuery() method that belongs to Statement interface for executing any database queries. It returns the instance of ResultSet interface that holds the records fetched from the query.

    e.g.

    ResultSet result=st.executeQuery("select * from student"); // suppose there is a student table in database  
    
    while(result.next())   
    {   
    System.out.println(result.getInt(1)+" "+result.getString(2));  
    }  
    

    5) Close the connection

    We use the close() method that belongs to Connection interface to close the connection object.

    e.g.

    con.close();
    

    Example to connect to oracle database:

     import java.sql.*;  
        class OracleCon{  
        public static void main(String args[]){  
        try{  
        //driver class  
        Class.forName("oracle.jdbc.driver.OracleDriver");  
    
        //connection object  
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","root","root");
        //The arguments contains url, db username and password which root and root over here  
    
        //statement object  
        Statement st=con.createStatement();  
    
        //query  
        ResultSet result=st.executeQuery("select * from student");    
    //ResultSet will hold the records fetched from student table  
        while(result.next())  
        System.out.println(result.getInt(1)+"  "+result.getString(2)+"  "+result.getString(3));  
    
        //close the connection  
        con.close();  
    
        }catch(Exception e){e.printStackTrace();}  
    
        }  
        }  
    

    Note: ojdbc14.jar file is required to be loaded for the above example.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: