0% found this document useful (0 votes)
8 views

DBMS Module 3

Uploaded by

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

DBMS Module 3

Uploaded by

rajendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

JDBC

What is JDBC?
JDBC stands for Java Database Connectivity, which is a
standard Java API for database-independent connectivity
between the Java programming language and a wide range
of databases.
• The JDBC library includes APIs for each of the tasks
mentioned below that are commonly associated with
database usage.
• Making a connection to a database.
• Creating SQL or MySQL statements.
• Executing SQL or MySQL queries in the database.
• Viewing & Modifying the resulting records
Common JDBC Components
• The JDBC API provides the following interfaces and classes
• DriverManager: This class manages a list of database
drivers. Matches connection requests from the java
application with the proper database driver using
communication sub protocol.
• The first driver that recognizes a certain subprotocol under
JDBC will be used to establish a database Connection.
• Driver: This interface handles the communications with the
database server. You will interact directly with Driver
objects very rarely.
• Instead, you use DriverManager objects, which manages
objects of this type. It also abstracts the details associated
with working with Driver objects.
• Connection: This interface with all methods for contacting a
database. The connection object represents communication
context, i.e., all communication with database is through
connection object only.

• Statement: You use objects created from this interface to


submit the SQL statements to the database. Some derived
interfaces accept parameters in addition to executing stored
procedures.

• ResultSet: These objects hold data retrieved from a database


after you execute an SQL query using Statement objects. It acts
as an iterator to allow you to move through its data.
• SQLException: This class handles any errors that occur in a
database application.
Two-tier and three-tier architecture

The JDBC API supports a two-tier and a three-tier architecture for


database access.

In a two-tier model, a Java application/applet communicates directly


with the database, via the JDBC driver.

The Java application/applet and the database can be on the same


machine, or the database can be on a server and the Java
application/applet can be on a client machine using any network
protocol

In a three-tier model, a Java application/applet communicates with a


middle tier component that functions as an application server. The
application server talks to a given database using JDBC
SQLJ
• What is static SQL and dynamic SQL?

• Static SQL is SQL statements in an application that do


not change at runtime and, therefore, can be hard-
coded into the application.

• Dynamic SQL is SQL statements that are constructed


at runtime; for example, the application may allow
users to enter their own queries. Thus, the SQL
statements cannot be hard-coded into the
application
What Is SQLJ?
SQLJ is an emerging database programming tool that
allows embedding of static SQL statements in Java
programs.

SQLJ is an attractive alternative to JDBC because it


allows translation-time syntax and semantics checking
of static SQL statements. As a consequence, application
programs developed in SQLJ are more robust.

SQLJ’s syntax is also much more compact than that of


JDBC, resulting in shorter programs and increased user
productivity.
Embed SQL statements in the Java program.
• Once the default connection has been
established, SQL statements can be embedded
within the Java program using the following
syntax:
#sql {<sql-statement>}

• where #sql indicates to the SQLJ translator, called sqlj, that


what follows is an SQL statement and <sql-statement> is any
valid SQL statement, which may include host variables and
host expressions
• The SQLJ translator takes as input an SQLJ program
file (with suffix .sqlj ) and produces a .java file along
with several other SQLJ profile files that contain the
classes necessary to perform the SQL operations.

• The translator also automatically invokes the Java


compiler to produce a .class file.
Case study
The Internet Book shop

Books(isbn char(10),title char(8),author char(80),


Qty_in_stock int, price int ,pub_year int)

Customer(cid int, cname char(80),address char(200))

Orders(orderno int,isbn char(10),cid int, cardnum char(16),


qty int,order_date DATE,ship_date DATE)
• Consider the types of queries and updates that
will arise.
• Customers search books by author name, title
or ISBN.
• Customer add and delete books from a
shopping basket.
• Augment the customer with additional
information to capture login and password.
• Customer registers with the website.
• Registered customers can change their address.

You might also like