You are on page 1of 5

JDBC

Java Data Base Connectivity


Jdbc is used to do database programming using Java.
Till now in java we are just building user interface.
(i.e character user interface) or GUI using Swing or AWT
means using java we are just capturing the user inputs
from the user.
And same user input we are placing with in class objects.
but we know that object life time is application dependent.
so what if we need some way to store object data on permanent basis over secon
dary
storage medium ?.
ans : one solution to this is to use any rdbms and store object data in relation
al form.
Java help's in doing the same in very efficient manner using JDBC
JDBC can be thinkes as set of built in classes of java
All these classes are present in java.sql package.
Same package contains lots of built in classess and interface for database conne
ctivity.
These built classes and interface are collectively known as
JDBC API. (Application programming Interface).
API - can be thinked as collection of built in classes and interfaces.
--------------------------------------------------------
JDBC API is a Java API that can access any kind of tabular data, especially
data stored in a Relational Database.
JDBC helps you to write java applications that manage
these three programming activities.
1. Connect to a data source.
(using Connection interface of java.sql package.)
2. Send queries and update statements to the database
(using Statement interface /or using PreparedStatement interface)
Select * from emp where dept='payroll';
insert into emp values('abcd',23,12000);
update emp set age=25 where name='abcd';
3. Retrieve and process the results received from the database in
response to your query (using ResultSet interface)
We can understand Jdbc in layered form also known as JDBC architecture
discuss the layers
1. Presentation layer : is the user interface of the applic
ation
presentation layer is the origin of request.
present results to end user.
over presentation layer either we will capture the data from th
e end user. or
we show resulted data to the end user.
presentation layer will request connection to the back end data
base.
presenation layer will send request to shutdown the connection,after whole work
get done.
after getting connected via presention layer we can send request and we can
get response .
if any exception occur during getting connected or during request/response
all these errors are also reported by presentation layer.
we can develop presentation logic using any
frontend tech. of java e.g
1. core java (CUI)
2. javascript/html (web based GUI)
3. jsp (web based GUI)
4. applet (web based GUI)
5. swings (two tier based GUI)
--------------------------------------------------------------------------------
----
then Connectivity drivers layer will come.
same layer is broken in two parts i.e.
1. Front end connectivity drivers.
2. Back end connectivity drivers.
What do u mean by Connectivity Drivers.
Connectivity driver are simple piece of software.
Connectivity drivers are used by front end tech
to interact with backend software. i.e database
Connectivity drivers provides communication
between front end and back end.
every rdbms vendor will develop his own connectivity
driver.(we can trace the same in datasource odbc)
and every front end tech. writes lots of connectivity
drivers in their own tech. to interact with different
databases.
front end tech. ko ek se jyada connectivity drivers
kyu req. hai. Because using front end we may req. to
pick data either from oracle/access/sqlserver/mysql.
Data base layer
represent repositorary of data any database access / oracle / sqlserver
<<<<<<<<how to create an jdbc application.>>>>>>>>>>
at first create a blank data base in ms access.
Then create data source name (dsn) for same database
dsn is the mix of backend data base physical location
+ reference of backend connectivity drivers.
steps for creating dsn(data source name)
start -> control panel -> /performance and maintain --> admin tool -> datas
ource(odbc) -> systemdsn ->
then click add button
then select appropriate driver from the list
then purpose name of dsn
then select physical location of .mdb file
Connection
Connection is built in interface of java present in sql package.
An object of connection interface provide a session with specific dat
abase.
SQL statements are executed and results are returned within the context
of a
connection object.
(means sql statements will work under Connection interface object).
A Connection object's provide information abt the connected database.
using object of connection interface we can obtain information about
tables present with in database.
Same information we can obtain using
getMetaData() method of connection interface.
object of Connection interface get initialized using getConnection()
method of DriverManager class. e.g ->
Connection conn;
conn=DriverManager.getConnection("jdbc:odbc:<dsn name>");
connection object represent physical connection of frontend with backend
db.
obtaining connection is the primary action.
after getting connection,we can start communication with backend databas
e.
communation with backend means sending sql statements to the database.
please ensure that before calling DriverManager.getConnection() method
at first we have to load java connectivity drivers.
to load java connectivity drivers issue following call
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
--------------------------------------------------------------------------------
------------------
// here Class is the built in class of java
// Class class is present in java.lang
// forName() is the static method of Class class
// notice C of Class is capital.
// forName method loads the JdbcOdbcDriver class with in the primary
// memory
// JdbcOdbcDriver class represent all connectivity drivers of java.
// JcbcOdbcDriver are also known as bridge driver of java

--------------------------------------------------------------------------------
-----------
DriverManager class
The basic service of driver manager class is to manage JDBC drivers.
DriverManager class will attempt to load the driver classes
This allows a user to customize the JDBC Drivers used by their applications.
there is a special method of Driver manager class i.e getConnection()
when getConnection is called, the DriverManager will attempt to locate a suitabl
e driver from amongst those loaded at initialization and those loaded explicitly
using the same classloader as the current applet or application.
getConnection java ke us driver ka selection karta hai jo mentioned
backend drivers ke saath work kar sakta hai
after this getConnection laid out connection between front end and
back end and reference of same connection will be awarded to
object of Connection interface as in the current case
connection reference goes to "con" object.
in future all transaction with backend in java application will
be carried out using "con" object.

---------------------------------------------------------------------
Statement Interface.
The object of statement interface is used for executing a static SQL statement a
nd returning the results which query may produce.
after executing sql statement it may be that result will come from data base.
e.g
if we issue select * from emp where deptno=20
then n numbers of rows will come from the backend data base.
so where we hold the result which is coming from statement
answer is with in ResultSet object.
ResultSet ob=st.executeQuery("select * from emp");
here st is the object of Statement interface.
executeQuery is the built in method of Statement interface.
Statement interface ka object initialize kaisai karte hai
Connection conn;
conn=DriverManager.getConnection(jdbc:odbc:<dsn name>);
// and after configuring conn object
Statement st=conn.createStatement();
// createStatement() is the built in method of Connection interface.
// createStatement() return Statement object.
// conn object kyu use kiya
// kyo ki sql ki query issue karte waqt wo kis context mai
execute hogi // same info will come from connection objec
t.
after configuring Statement object we are ready to
issue sql queries.
to issue sql command Statement interface provide b
uilt in methods
1. executeUpdate()
Executes the given SQL statement, which may
be an INSERT, UPDATE, or DELETE statement
or an SQL statement that returns nothing,
such as an SQL DDL statement.
Returns:
either the row count for INSERT, UPDATE
or DELETE statements,
or 0 for SQL statements that return nothing
can Throw
SQLException
2. executeQuery()
Executes the given SQL statement, which
returns a single ResultSet object.
Parameters:
sql - an SQL statement to be sent to the database, typically a static SQL SELE
CT statement
Returns:
a ResultSet object that contains the data produced by the given query; never n
ull
Throws:
SQLException - if a database access error occurs or the given SQL s
tatement produces anything other than a single ResultSet object

You might also like