Connecting to Oracle Database

For connecting java application with the oracle database, we need to follow the above steps . Here I am using Oracle10g as the database. So we need to know following information for the oracle database:

  • Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
  • Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where 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. You may get all these information from the tnsnames.ora file.
  • Username: The default username for the oracle database is system.
  • Password: Password is given by the user at the time of installing the oracle database.

Let's see an example to retrieve all the records of employee table. So we have to first create a table in oracle database, then inserting some records, then writing a jdbc application to retrieve records from oracle database.

//Creating table
create table employee
(
id number(10),
name varchar2(40),
sal number(7)
);
inserting a record
insert into employee values(101,'Tapuuu',15000);

//JDBC Program
import java.sql.*;
class JdbcDemo
{
public static void main(String args[ ]) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "password");
Statement st=conn.createStatement( );
ResultSet rs = rs.executeQuery("select * from employee");
while(rs.next( ))
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
}
con.close( );
}
}

Output:
101 Tapuuu 15000 Note:

To connect java application with the Oracle database, ojdbc14.jar file is required to be loaded.

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment