Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Example of Oracle JDBC Connection

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 234
    Comment on it

    We have different database vendors out in the market and every vendor has different configuration for using their database. Here in this article we will talk about the getting connection of Oracle database using Jdbc.


    For getting connection of Oracle database we need following information :

    driver class name : Oracle driver class name is oracle.jdbc.driver.OracleDriver
    connection url : connection url for Orcale is jdbc:oracle:thin:@localhost:1521:testDB
    username : username of the database
    password : password of the database


    Example of creating Oracle database connection : In the following example I will show you how to connect to the Oracle database using JDBC in Java.

    DbConnectionOfOracle.java

    package com.dbconnection.;
    
    import java.sql.*;
    
    class DbConnectionOfOracle {
    
        public static void main(String args[]) {
            try {
                // load the driver class
                Class.forName("oracle.jdbc.driver.OracleDriver");
    
                // create the connection object
                Connection con = DriverManager.getConnection(
                        "jdbc:oracle:thin:@localhost:1521:testDB", "username", "password");
                System.out.println("Connection is created successfully.");
                //or can create connection like this
                /*  Connection con = DriverManager.getConnection(
                        "jdbc:oracle:thin:@localhost:1521:xe:testDB", "username", "password");*/
    
                // create the statement object
                Statement stmt = con.createStatement();
    
                // run query
                ResultSet rs = stmt.executeQuery("select * from user");
                while (rs.next()){
                    System.out.println("User ID : " + rs.getInt(1));
                    System.out.println("Username : "+ rs.getString(2));
                }
    
                // close the connection
                con.close();
    
            } catch (Exception e) {
                System.out.println("Error while creating connection.");
                e.printStackTrace();
            }
        }
    }
    

    Output :

    Connection is created successfully.
    User ID : 11
    Username : sumit
    

 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: