JDBC
Java database connectivity
Java and database(build application)
Introduction
A database is an organized collection of data. There
are many different strategies for organizing data to
facilitate easy access and manipulation
A database management system (DBMS) provides
mechanisms for storing, organizing, retrieving and
modifying data for many users
Database management systems allow for the access
and storage of data without concern for the internal
representation of data.
Java and database(build application)
Java programs communicate with databases and
manipulate their data using the JDBC API.
A JDBC driver enables Java applications to connect to a
database in a particular DBMS and allows programmers
to manipulate that database using the JDBC API.
JDBC is almost always used with a relational
database. However, it can be used with any table-
based data source
Some popular
relational database management systems (RDBMSs) are
Microsoft SQL Server, Microsoft access, Oracle,
Sybase, IBM DB2, Informix, PostgreSQL and MySQL
Java and database(build application)
relational database is a logical representation of
data that allows the data to be accessed without
consideration of its physical structure
A relational database stores data in tables
Tables are composed of rows, and rows are
composed of columns in which values are stored
primary key a column (or group of columns) in a
table with a unique value that cannot be duplicated
in other rows.
Java and database(build application)
Employee table
Java and database(build application)
SELECT * FROM patient
JDBC – Java Database Connectivity
Introduction to JDBC
□JDBC is used for accessing databases from
Java applications
□Information is transferred from relations to objects
and vice-versa
databases optimized for searching/indexing
objects optimized for engineering/flexibility
7
JDBC Architecture
Oracle
Driver
Oracle
Java Application DB2
JDBC
Driver
DB2
Network
MySQL
Driver
We will
use this one… MySQL
8
JDBC Architecture (cont.)
Application JDBC Driver
• Java code calls JDBC library
• JDBC loads a driver
• Driver talks to a particular database
• An application can work with several databases by using all
corresponding drivers
• Ideal: can change database engines without changing
any application code (not always in practice)
JDBC Driver for MySQL (Connector/J)
• Download Connector/J using binary distribution from
:
[Link]
ml
• To install simply unzip (or untar) and put
mysql-connector-java-[version]-[Link] (I have
installed [Link]) in the
class path
• For online documentation, see :
[Link]
-[Link]
10
Seven Steps to establish connection
• Load the driver
• Define the connection URL
• Establish the connection
• Create a Statement object
• Execute a query using the
Statement
• Process the result
• Close the connection
11
Loading the Driver
• We can register the driver indirectly using the
statement
[Link]("[Link]");
• [Link] loads the specified class
• When mysqlDriver is loaded, it automatically
– creates an instance of itself
– registers this instance with the DriverManager
• Hence, the driver class can be given as an argument of
the application
12
Connecting to the Database
• Every database is identified by a URL
• Given a URL, DriverManager looks for the
driver that can talk to the corresponding
database
• DriverManager tries all registered drivers, until
a suitable one is found
[Link]("[Link]");
[Link]("jdbc:mysql://
localhost:3306/databasename","root","");
13
Interaction with the Database
• We use Statement objects in order to
– Query the database
– Update the database
• Three different interfaces are used:
Statement, PreparedStatement,
CallableStatement
• All are interfaces, hence cannot be instantiated
• They are created by the Connection
prepareStatement("SELECT * FROM patient");
You have to import all library classes required for the project
like
import [Link].*;
Querying with Statement
String queryStr =
"SELECT * FROM employee
" + "WHERE lname =
‘Ahmed'";
Statement stmt = [Link]();
ResultSet rs = [Link](queryStr);
• The executeQuery method returns a ResultSet
object representing the query result.
• Will be discussed later…
15
Changing DB with Statement
String deleteStr =
"DELETE FROM employee "
+ "WHERE lname =
‘Ahmed'";
Statement stmt = [Link]();
int delnum = [Link](deleteStr);
• executeUpdate is used for data manipulation: insert, delete, update, create table,
etc. (anything other than querying!)
• executeUpdate returns the number of rows modified
16
inserting data into MYSQL table
[Link]("[Link]");
conn=[Link]("jdbc:m
ysql://localhost:3306/dbname","root","");
prepareStatement("INSERT INTO patient
VALUES(?,?,?,?,?,?,?)");
Inserting data from Jform
import [Link].*;
Connection conn=null;
PreparedStatement st=null;
[Link]("[Link]");
conn=[Link]("jdbc:mysql://localhost:3306/
dbname","root","");
st=[Link]("INSERT INTO patient
VALUES(?,?,?,?,?,?,?)");
[Link](1,[Link]());
[Link](2,[Link]());
[Link](3,[Link]());
[Link](4,[Link]());
[Link](5,[Link]());
[Link](6,[Link]());
[Link]();