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

Antony - Java Database Programming JDBC Connections

The document discusses the Java Database Connectivity (JDBC) API for connecting Java applications to databases. It covers loading JDBC drivers, establishing connections via a connection URL, creating statements to execute SQL, processing result sets returned from queries, and closing connections. Specific examples are provided for connecting to Access, MySQL, and Oracle databases.

Uploaded by

Antony
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
53 views

Antony - Java Database Programming JDBC Connections

The document discusses the Java Database Connectivity (JDBC) API for connecting Java applications to databases. It covers loading JDBC drivers, establishing connections via a connection URL, creating statements to execute SQL, processing result sets returned from queries, and closing connections. Specific examples are provided for connecting to Access, MySQL, and Oracle databases.

Uploaded by

Antony
Copyright
© © All Rights Reserved
You are on page 1/ 24

A.

Ngorora

JAVA DATABASE PROGRAMMING –


JDBC CONNECTIONS

JDBC 1
Agenda

 JDBC
 load driver
 Connect to a database
 Create statements
 Execute statements
 Process results
 Explore database metadata

JDBC 2
JDBC

 JDBC is the Java API for accessing relational


databases
 Provides Java programmers with a uniform
interface for accessing and manipulating a wide
range of relational databases.
 Java programs can then execute SQL statements,
retrieve results, present data in a user-friendly
interface, and propagate changes back to the
database
 Can also be used in heterogeneous environment
JDBC 3
JDBC API

 The JDBC API is a set of interfaces and classes


used to write Java programs for accessing and
manipulating relational databases

JDBC 4
JDBC Drivers

 A JDBC driver serves as the interface to


facilitate communication between JDBC and
a proprietary database
 Database specific
 Normally provided by the database vendors

JDBC 5
RELATIONSHIPS

JDBC 6
Drivers
Database Driver class Source

Access Sun.jdbc.odbc.JdbcOdbc Already in JDK


Driver

mySQL com.mysql.jdbc.Driver Companion web site

Oracle Oracle.jdbc.driver.Oracle Companion web site


Driver

JDBC 7
Steps to access database

 Load drivers
 Establish connections
 Creating statements
 Executing statements
 Processing ResultSet
 Close connection

JDBC 8
1 - Load drivers

 Statement:
 Class.forName(“……”);
 Driver is concrete class that implements the
java.sql.Driver interface
 List all drivers for all databases you want to
access

JDBC 9
2 - Connection

 To connect to a database use static method in


the DriverManager class
 getConnection(databaseURL)
 Connection connection =
DriverManager.getConnection(databaseURL);
 databaseURL is the unique identifier of the
database on the Internet

JDBC 10
Connection - cable

Program

Databases

JDBC 11
JDBC URLs

Database URL pattern

 Access  jdbc:odbc:dataSource

 MySQL  jdbc:msql://hostname/dbname

 Oracle  jdbc:oracle:thin:@hostname:port#:oracleDBSID

JDBC 12
Example: Access database

 Needs an ODBC data source

 Connection connection =
DriverManager.getConnection(“jdbc:odbc:
ExampleMDBDataSource”);

JDBC 13
Example: MySQL database

 Specifies host name and db name

 Connection connection =
DriverManager.getConnection
(“jdbc:mysql://localhost/javabook”, “scott”,
“tiger”);

 Database name: javabook


 Username: scott
 Password: tiger
JDBC 14
Example: Oracle database

 Specifies: hostname, port#, oracleDBSID

 Connection connection =
DriverManager.getConnection
(“jdbc:oracle:thin:@liang.armstrong.edu:1521:
orcl”, “scott”, “tiger”);
 Database on liang.armstrong.edu

JDBC 15
3 – Creating statements

 Statement statement =
connection.createStatement();

JDBC 16
Statements - cart

Program

SQL
statements

Results

Databases

JDBC 17
4 – Executing statements

 Three options:

 SQL data definition language (DDL) and update


statements
 SQL query statements
 Result of query

JDBC 18
Update command

SQL: create Table Temp (col1 char(5),


col2 char(5))

statement.executeUpdate(“create
Table Temp (col1 char(5), col2 char(5))”);

JDBC 19
Query command

 SQL: select firstName, mi, lastName from


Student where lastName = ‘Smith’

 ResultSet resultSet =
statement.executeQuery(“select firstName,
mi, lastName from Student where lastName
+ ‘ = Smith’”);

JDBC 20
5 – Processing ResultSet

 ResultSet maintains a table whose current


row can be retrieved
 The initial row position is null
 Can use next method to move to next row
 Get methods to retrieve values from a current
row

JDBC 21
Example

while(resultSet.next())
S.o.p(resultSet.getString(1) + “ ” +
resultSet.getString(2) + “ ”
resultSet.getString(3);

 getString(1) … retrieve the column values


 Could also
 getString(“firstName”, .....

JDBC 22
6 – Close connection

 connection.close();

JDBC 23
The end…..

JDBC 24

You might also like