0% found this document useful (0 votes)
13 views19 pages

JDBC

The document provides an overview of Java Database Connectivity (JDBC), detailing how Java applications interact with databases using the JDBC API and drivers. It explains the architecture of JDBC, the steps to establish a database connection, and how to execute queries and manipulate data. Additionally, it includes specific examples of connecting to a MySQL database and performing operations such as inserting and deleting records.

Uploaded by

mohamedliban972
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)
13 views19 pages

JDBC

The document provides an overview of Java Database Connectivity (JDBC), detailing how Java applications interact with databases using the JDBC API and drivers. It explains the architecture of JDBC, the steps to establish a database connection, and how to execute queries and manipulate data. Additionally, it includes specific examples of connecting to a MySQL database and performing operations such as inserting and deleting records.

Uploaded by

mohamedliban972
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/ 19

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


:
http://dev.mysql.com/downloads/connector/j/5.0.ht
ml
• To install simply unzip (or untar) and put
mysql-connector-java-[version]-bin.jar (I have
installed mysql-connector-java-5.0.4-bin.jar) in the
class path
• For online documentation, see :
http://dev.mysql.com/doc/refman/5.0/en/connector
-j.html
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
Class.forName("com.mysql.jdbc.Driver");
• Class.forName 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

Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("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 java.sql.*;
Querying with Statement
String queryStr =
"SELECT * FROM employee
" + "WHERE lname =
‘Ahmed'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(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 = con.createStatement();
int delnum = stmt.executeUpdate(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
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:m
ysql://localhost:3306/dbname","root","");
prepareStatement("INSERT INTO patient
VALUES(?,?,?,?,?,?,?)");
Inserting data from Jform
import java.sql.*;
Connection conn=null;
PreparedStatement st=null;
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/
dbname","root","");
st=conn.prepareStatement("INSERT INTO patient
VALUES(?,?,?,?,?,?,?)");
st.setString(1,fname.getText());
st.setString(2,lname.getText());
st.setString(3,email.getText());
st.setString(4,city.getText());
st.setString(5,phone.getText());
st.setString(6,dep.getText());
st.executeUpdate();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy