0% found this document useful (0 votes)
7 views5 pages

Database Connectivity

The document outlines the five essential steps to connect a Java application to a database using JDBC: registering the driver class, creating a connection, creating a statement, executing queries, and closing the connection. It provides specific examples using the Oracle database, including the necessary driver class, connection URL, username, and password. Additionally, it explains how to create a table and load the required ojdbc14.jar file for establishing the connection.

Uploaded by

sohampatil200616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Database Connectivity

The document outlines the five essential steps to connect a Java application to a database using JDBC: registering the driver class, creating a connection, creating a statement, executing queries, and closing the connection. It provides specific examples using the Oracle database, including the necessary driver class, connection URL, username, and password. Additionally, it explains how to create a table and load the required ojdbc14.jar file for establishing the connection.

Uploaded by

sohampatil200616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

There are 5 steps to connect any java application with the database using JDBC.

These
steps are as follows:
o Register the Driver class
o Create connection
o Create statement
o Execute queries
o Close connection

1) Register the driver class

The forName() method of Class is used to register the driver class. This method is used to
dynamically load the driver class.

Syntax of forName() method

public static void forName(String className)throws ClassNotFoundException

Example to register the OracleDriver class


Here, Java program is loading oracle driver to esteblish database connection.
[Link]("[Link]");

2) Create the connection object

The getConnection() method of DriverManager class is used to establish connection with the
database.

Syntax of getConnection() method

1) public static Connection getConnection(String url)throws SQLException


2) public static Connection getConnection(String url,String name,String password)
throws SQLException

Example to establish connection with the Oracle database


Connection con=[Link]( "jdbc:oracle:thin:@localhost:1521:xe","s
ystem","password");

3) Create the Statement object

The createStatement() method of Connection interface is used to create statement. The object
of statement is responsible to execute queries with the database.

Syntax of createStatement() method

public Statement createStatement()throws SQLException

Example to create the statement object

Statement stmt=[Link]();

4) Execute the query


The executeQuery() method of Statement interface is used to execute queries to the database.
This method returns the object of ResultSet that can be used to get all the records of a table.

Syntax of executeQuery() method

public ResultSet executeQuery(String sql)throws SQLException

Example to execute query

ResultSet rs=[Link]("select * from emp");

while([Link]()){
[Link]([Link](1)+" "+[Link](2));
}

5) Close the connection object

By closing connection object statement and ResultSet will be closed automatically. The close()
method of Connection interface is used to close the connection.

Syntax of close() method

public void close()throws SQLException

Example to close connection


[Link]();
Java Database Connectivity with Oracle
To connect java application with the oracle database, we need to follow 5 following steps. In this example,
we are using Oracle 10g as the database. So we need to know following information for the oracle database:

1. Driver class: The driver class for the oracle database is [Link].
2. 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
[Link] file.
3. Username: The default username for the oracle database is system.
4. Password: It is the password given by the user at the time of installing the oracle database.

Create a Table

Before establishing connection, let's first create a table in oracle database. Following is the SQL query to
create a table.

create table emp(id number(10),name varchar2(40),age number(3));

To connect java application with the Oracle database [Link] file is required to be
loaded.

download the jar file [Link]

Two ways to load the jar file:

1. paste the [Link] file in jre/lib/ext folder


2. set classpath

1) paste the [Link] file in JRE/lib/ext folder:

Firstly, search the [Link] file then go to JRE/lib/ext folder and paste the jar file here.
2) set classpath:

There are two ways to set the classpath:


o temporary
o permanent

How to set the temporary classpath:

Firstly, search the [Link] file then open command prompt and write:

1. C:>set classpath=c:\folder\[Link];.;

How to set the permanent classpath:


Go to environment variable then click on new tab. In variable name write classpath and
in variable value paste the path to [Link] by appending [Link];.; as
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\[Link];.;

You might also like