Connect Java with Oracle Database

Advertisements

Prev Tutorial Next Tutorial

How to Connect Java Application with Oracle Database

Oracle corporation has provided two Jdbc driver software for connection java application to a database of oracle server.

  • Oracle oci Driver
  • Oracle thin Driver
connect java with oracle

For connecting java application with the oracle database, we need to follow below steps. In this example we are using Oracle 10g as the database. So we need to know following information for the oracle database.

The following are the connection properties of oracle thin driver.

Driver Class: Oracle.Jdbc.OracleDriver
Connection url: jdbc:oracle:thin:@localhost:1521:xe
Username:The default username for the oracle database is system.
passwordPassword is given by the user at the time of installing the oracle database.

jdbc:oracle:thin:@localhost:1521:x

  • jdbc is the API
  • oracle is the database
  • thin is the driver
  • localhost is the server name on which oracle is running, we may also use IP address
  • 1521 is the port number and
  • XE is the Oracle service name.

Note: You may get all above information from the tnsnames.ora file.

Create a table in oracle database

create table student(roll number(10),name varchar2(40));  

Example to Connect Java Application with Oracle database

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","oracle");  
  
//step3 create the statement object  
Statement stmt=con.createStatement();  
  
//step4 execute query  
ResultSet rs=stmt.executeQuery("select * from student");  
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);
}  
}  
}  

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements