JDBC: Connection With Oracle Database


For connecting with the Oracle database from JAVA application, you need to follow 5 steps to perform database connectivity. Below are connection information specific to oracle database:

  1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
  2. Connection URL: The connection URL for the oracle database is jdbc:oracle:thin:@localhost:1521:FacingIssuesOnITDB here jdbc is the API, oracle is the database, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and FacingIssuesOnITDB is the database name. We may use any database, in such case, you need to replace the FacingIssuesOnITDB with your database name.
  3. Username: The default username for the oracle database is root.
  4. Password: Password is given by the user at the time of installing the oracle database. In this example, we are going to use root as the password.
import java.sql.*;
class OracleConnection{
public static void main(String args[]){
try{
//Step 1: Register the driver class
 Class.forName("oracle.jdbc.driver.OracleDriver");
//Step 2: Create connection with database
   Connection con=DriverManager.getConnection(
   "jdbc:oracle:thin@localhost:1521:FacingIssuesOnITDB","root","root");
//here FacingIssuesOnITDB is database name, root is username and password
//Step 3: Create statement
 Statement stmt=con.createStatement();
//Step 4: Execute query
 ResultSet rs=stmt.executeQuery("select * from emp");   

while(rs.next()){
  System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
}
//Step 5: close connection
con.close();
    }
catch(ClassNotFoundException e){
System.out.println(e);
}
catch(SQLException e){
 System.out.println(e);
    }
}

Here you learn about steps to connect database with application and specific configuration information for Oracle.

See also :

More on JDBC

Follow below links to know more on JDBC and solving JDBC issues :

JDBC Tutorial

JDBC Sample Code

JDBC Issues and Solutions

JDBC Interview Questions And Answer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s