Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JDBC 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)
);
For inserting a record, the required sql statement is:
insert into employee values(101,’Rosalin’,15000);

JdbcExample1.java 
import java.sql.*; 
class JdbcExample1
{
public static void main(String[] args) throws Exception
{
    Class.forName(“oracle.jdbc.driver.OracleDriver”);
    Connection con = DriverManager.getConnection(“jdbc:oracle:thin:@ 
    localhost:1521:xe”, “system”, “password”);
    Statement st=con.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 Rosalin 15000

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


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc







 PreviousNext