Chapter 3 - Database Connectivity
Chapter 3 - Database Connectivity
Connectivity
Pre-Knowledge 2
Table
Manipulating Table
Insert
Update
Select
Delete
Using Joins
Stored Procedures
Objectives 3
know how to use JDBC to execute SQL queries and how to handle the
results returned;
Overview 4
A database is a means of storing information in such a way
Overview of SQL 7
SELECT
SELECT **
FROM
FROM Product
Product “selection”
WHERE
WHERE category=‘Gadgets’
category=‘Gadgets’
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
([]-
optional, | - or)
Advanced Programming Chapter Three:
Database Connectivity
9
4. Executing Statement
5. Processing Result
6. Closing Connection
Step 1: Loading JDBC
Driver
IN ORDER TO USE JDBC FOR THE ACCESSING OF DATA FROM A PARTICULAR TYPE OF
RELATIONAL DATABASE, IT IS NECESSARY TO PROVIDE SOME MEDIATING SOFTWARE THAT
WILL ALLOW JDBC TO COMMUNICATE WITH THE VENDOR-SPECIFIC API FOR THAT DATABASE.
SUCH SOFTWARE IS REFERRED TO AS A DRIVER.
A JDBC Driver 14
Suitable drivers are usually supplied either by the database
Types 3 and 4:
Pure Java implementations and require no additional
software to be installed on the client, except for the
JDBC driver.
Fortunately, packaging the JDBC driver with your
software distribution is trivial.
Type 1: JDBC−ODBC bridges 16
Pros
Cons
A vendor−specific API must be installed on client computer.
The highest performance driver available for the database and is usually
provided by the vendor itself.
Type 4: 100% Java 24
Pros
No additional client software required.
Direct communication with database server.
Cons
May require additional configuration for Internet use.
Adding Driver 25
Database Connectivity
Thursday, March 13, 2025
Adding Driver in NetBeans
Loading JDBC Driver 27
A driver is a concrete class that implements the
Database Connectivity
Thursday, March 13, 2025
Loading JDBC Driver Example
Access Loaded JDBC Driver 30
Database Connectivity
Thursday, March 13, 2025
Step 2: Establishing
connection
Define Connection URL 32
JDBC URLs provide a way to identify a database.
Example
jdbc:mysql//hostname/databaseName
jdbc:oracle:thin@hostname:portnumber:databaseName
Some Popular JDBC URLS 33
MySQL jdbc:mysql://hostname/databaseName
Oracle jdbc:oracle:thin@hostname:portnumber:databaseName
JavaDB jdbc:derby://localhost:1527/databaseName
Access jdbc:odbc:databaseName
PostGreSQL jdbc:postgres://host/database
SQL Server jdbc:sqlserver://[serverName[\instanceName][:portNumber]]
[;property=value[;property=value]]
Establishing Connection with 34
JDBC
Database Connectivity
Thursday, March 13, 2025
Driver Manager
Advanced Programming Chapter Three:
38
Database Connectivity
Thursday, March 13, 2025
Step-3 and 4: Creating and
Executing Statement
Creating Statement 39
Statement objects allow you to execute basic SQL queries and retrieve the results through
Syntax:
Prepared Statement 45
It has three main uses
If parameters for the query are not set the driver returns an SQL
Exception
We must supply values for every parameter before executing the SQL
statement.
setXXX(int position, XXX value) methods bind values to the parameters, where
XXX represents the Java data type of the value you wish to bind to the input
parameter.
The first marker represents position 1, the next position 2, and so forth.
Advanced Programming Chapter Three:
47
Database Connectivity
Thursday, March 13, 2025
Data Types Mapping
Prepared Statement 48
Database Connectivity
Thursday, March 13, 2025
Executing Prepared statement
(Execute Update)
Advanced Programming Chapter Three:
51
Database Connectivity
Thursday, March 13, 2025
Executing Prepared statement
(execute Query)
Callable Statement interface 52
Designed to execute SQL-stored procedures.
The section between BEGIN and END is called the body of the
stored procedure.
Stored Procedures in MySQL 59
Mode:
IN: is the default mode. When you define an IN parameter in a stored procedure, the
calling program has to pass an argument to the stored procedure.
OUT: the value of an OUT parameter can be changed inside the stored procedure and its
new value is passed back to the calling program.
INOUT: is a combination of IN and OUT parameters. It means that the calling program may pass
the argument, and the stored procedure can modify the INOUT parameter, and pass the new value
Invoking procedure:
Advanced Programming Chapter Three:
60
Database Connectivity
Thursday, March 13, 2025
Stored Procedures in MySQL
Example 1
Advanced Programming Chapter Three:
61
Database Connectivity
Thursday, March 13, 2025
Stored Procedures in MySQL
Example 2
Advanced Programming Chapter Three:
62
Database Connectivity
Thursday, March 13, 2025
Executing Callable Statement
Example
Advanced Programming Chapter Three:
63
Database Connectivity
Thursday, March 13, 2025
Step-5: Processing Result
ResultSet 64
The SQL statements that read data from a database query, return the
To move the cursor to the first row of data next() method is invoked on the resultset
If the next row has a data the next() results true else it returns false and the
cursor moves beyond the end of the data
Returns true if the cursor is now positioned on a row and false if the cursor is
positioned after the last row.
ResultSet: Cursors 66
first: Moves the cursor to the first row in the ResultSet object.
last: Moves the cursor to the last row in the ResultSet object.
Returns true if the cursor is now positioned on the last row and false if the ResultSet
object does not contain any rows.
beforeFirst: Positions the cursor at the start of the ResultSet object, before the first
row.
If the ResultSet object does not contain any rows, this method has no effect.
afterLast: Positions the cursor at the end of the ResultSet object, after the last row.
If the ResultSet object does not contain any rows, this method has no effect.
getFloat(), getDouble()
getClob(), getBlob(),
Not updateable
resultSetConcurrency);
prepareCall(String sql(call), int resultSetType, int resultSetConcurrency);
ResultSet.TYPE_FORWARD_ONLY
Note:
resultSetType indicates how scrollable, and how sensitive to data changes
By using batch processing, these queries can be sent to the database in one
call, thus improving performance.
4. Add as many as SQL statements you like into batch using addBatch() method on
created statement object.
5. Execute all the SQL statements using executeBatch() method on created statement
object.
Advanced Programming Chapter Three:
71
Database Connectivity
Thursday, March 13, 2025
Example
Advanced Programming Chapter Three:
72
Database Connectivity
Thursday, March 13, 2025
Step-6: Closing Connection
Closing Connection 73
Each machine has a limited number of connections
Java Metadata information is useful for writing code that can adapt to several
database system or to the content of any database.
creating a sort of generic interface that uses advanced database features to discover
database metadata at runtime.
What keywords are used by the database that are not SQL2?
Database Connectivity
Thursday, March 13, 2025
Creating Database MetaData
Creating DatabaseMetaData
…
ResultSet MetaData 77
String.
Programming
getColumnCount() Returns the number of columns in the result set.
Returns an int.
getColumnName(int n) Returns the name of a column at position n in the
Chapter
result set. Returns a String.
getColumnType(int n) Returns the JDBC data type for a column at
Three:
position n of the result set. Returns an int.
getColumnTypeName(int n) Provides the name of the column’s data
Database Connectivity
Thursday, March 13, 2025
Creating ResultSet MetaData
Advanced Programming Chapter Three:
79
Database Connectivity
Thursday, March 13, 2025
End of Chapter 3