You are on page 1of 6

Online Shopping

2.2 DATABASE CONNECTIVITY

A database is collection of interrelated data is contiguous.


In some organized fashion and DBMS (Data Base Management
System) is software that provides you with a mechanism to retrieve,
modify, and add data to database. A table is unit of storage, which
holds data in the form of rows and columns. Thus, a collection of all
tables with their interrelationship could be termed as database. The
DBMS whose design is based on the Relational theory is called
RDBMS (Relational Database Management System). There are many
DBMS/RDBMS products available, for example Oracle, Sybase,
Informix,
MS-SQL Server, Ingress and MS-Access. Every RDBMS store data
in its own format. MS-Access stores data in MDB file format.

JDBC (Java Database Connectivity) is defined, as a set of java


classes and methods to interface with database. It also provides
uniform access to a wide range of relational databases. The java2
software bundle includes JDBC and the JDBC-ODBC Bridge.

JDBC:
For any application to communicate with the database, it needs
to have the following information:

1. The RDBMS/DBMS product is used to which the database is


created.
2. Where database reside. (Location)
3. The name of the database.

23

Dept of C.S.E.,Narayana Engineering College.


Online Shopping

So, we use JDBC calls to retrieve and update information from a data
base use JDBC. This involves the following functions:

1. Opening and establish a database connection


2. Send SQL statements.
3. Process the returned result set.
4. Close the database connection.

ODBC API:

Microsoft ODBC (Open Data Base Connectivity)


Application programming interface is probably the most widely used
programming interface for accessing RDBMS. ODBC is written
entirely in C, JDBC is written in Java. But, both JDBC and ODBC are
based on the X/Open SQL Command Level Interface. This is an
interface to perform SQL calls to database. The SQL statement is
embedded in the statements.

Oracle
Application Data Base

ODBC

Oracle SQL SQL


Driver Driver Data base

24

Dept of C.S.E.,Narayana Engineering College.


Online Shopping

Fig. Overview Of Database

JDBC API:
Java Database Connectivity provides a database progamming
API for Java programs. Java programs cannot directly communicate
with the ODBC driver. Sun Microsystem provides a JDBC-ODBC
bridge that translate JDBC to ODBC.There are several type of JDBC
drivers available They are:

1. JDBC-ODBC bridge + ODBC driver


2. Native API partly Driver
3. JDBC-Net Pure java Driver
4. Native protocol pure java Driver.

THE STATEMENT OBJECT:

The statement object is created by calling the create Statement


()
Method from the connection object. The statement object is used to
execute simple Queries. It has three methods that can be used for the
purpose of querying.

1.execute Query ( )

25

Dept of C.S.E.,Narayana Engineering College.


Online Shopping

This is used to execute simple select query and return a single


Result Set object
.

2.execute Update ()

This is used to execute SQL Insert, update and Delete


statements

3.execute( )

It is used to execute SQL statements that may return multiple values.

In the above program we first import the java.sqlpackage. The


JDBC-ODBC bridge is a bridge driver that is loaded by
Class.forName() method. The Connection object is intialized by get
Connection ()
Method. Then, statement is created. Next we execute a simple select
query using the execute Query () method of the statement object(st).

PREPARED STATEMENTS:
The prepared Statement interface descends from the
Statement interface and uses a template to create a SQL request. Use
a Prepared Statement to send precompiled SQL statements with on or
more parameters.

Query PreparedStatement:

You can create a Prepared Statement object by specifying the


template definition and parameter parameter placeholders. The

26

Dept of C.S.E.,Narayana Engineering College.


Online Shopping

parameter data is inserted into the Prepared Statement object by


calling its setXXX methods and specifying the parameter and its data.
The SQL instructions and parameters are sent to the database when
the executeXXX method is called.
This code segment creates a PreparedStatement object to
select user data based on the users login id. The question mark (“?”)
indicates this statement has one parameter.

PreaparedStatement pstmt = con.PrepareStatement(“Select theuser


from class where loginid like?”);

//initialize first parameter with loginid


pstmt.setString(1,”2msc15”);
ResultSet results = ps.executeQuery();

Once the PreparedStatement template is initialized, only the


changed values are inserted for each call.

Pstmt.setString(1,”2msc18”);

The? Symbol is a placeholder that can be replaced by the


input parameter at run time.

CALLABLE STATEMENT:

Once you have established a connection to a database, you


can use the prepareCall() method to create a callable statement. A
callable statement lets you execute SQL stored procedures. This
method handle one or more parameter as input arguments IN, OUT
and INOUT parameters. The following code is JDBC-ODBC bridge for

27

Dept of C.S.E.,Narayana Engineering College.


Online Shopping

GUI based data base manipulation. If you click the Show button, it
displays all rows in student table.

28

Dept of C.S.E.,Narayana Engineering College.

You might also like