SQL Oracle Java JSP JDBC MORE

How to connect to the oracle database in java JDBC


Connecting Java application with oracle database is very easy just follow the below step to perform database connectivity.

In this example I am using oracle database and fetch the data of students.

For connecting Java application with oracle database 4 things needed :

  1. Driver class
  2. JDBC Connection URL
  3. Username
  4. Password
  5. Jar File

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

2.JDBC Connection URL:  The JDBC connection URL for the oracle database is  jdbc:oracle:thin:@localhost:1521:xe

 

Example: jdbc:mysql://localhost:3306/students

  1. Here JDBC is the API.
  2. oracle is the database.
  3. Thin is the driver.
  4. Localhost is the server name on which oracle is running.
  5. 3306 is the port number

3.Username: Put your username if any.The default username for the mysql database is system.

4.Password: Password is given by the user at the time of installing the Oracle database. In this example, we are going to use Students@123 as the password.

5.Jar File: Download Here

Example:

import java.sql.*;  
class OracleCon{ 
  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:oracle:thin:@localhost:1521:xe","system","Students@123");  
   //step3 create the statement object  
Statement stmt=con.createStatement();  
  //step4 execute query  
ResultSet rs=stmt.executeQuery("select * from students_data”);
   while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
    //step 5 close the connection  
con.close();  
 }
catch(Exception e)
{  System.out.println(e);
}   } }