Chapter 3
Chapter 3
2
• Programs connect to, and interact with, relational databases a
software that facilitates communications between a database
management system and a program.
4
• JDBC architecture is composed of two key layers:
• JDBC driver: Acts as the bridge between the JDBC manager and
the specific database driver. JDBC drivers are database-specific
and are responsible for translating JDBC calls into commands that
the respective database understands.
5
o JDBC Driver is a software component that enables java application to
6
1) JDBC-ODBC bridge driver(Type 1)
7
1) JDBC-ODBC
Advantages:
•easy to use.
•can be connected to any database.
Disadvantages:
•Performance degraded because JDBC method call is converted
into the ODBC function calls.
•The ODBC driver needs to be installed on the client machine.
•This type of driver cannot talk to the database directly.
8
2) Native-API driver (Type 2)
The Native API driver uses the client-side libraries of the database. The driver
converts JDBC method calls into native calls of the database API. It is not
9
2) Native-API driver
Advantage:
Disadvantage:
10
3) Network Protocol driver (Type 3)
11
Network Protocol driver
Advantage:
•No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
Disadvantages:
12
4) Thin driver –Pure Java (Type 4)
oThe thin driver converts JDBC calls directly into the vendor-specific
database protocol. That is why it is known as thin driver. It is fully written
in Java language.
13
4) Thin driver
Advantage:
Disadvantage:
14
• The JDBC architecture is sometimes classified as: two-tier and three-tier.
Type 2 and 4 drivers use two-tier and Type 1 and 3 use three-tier
architecture.
• Figure 4.2. (i) and (ii) show the JDBC two-tier and three-tier
architectures.
15
• Java provides an API for accessing and processing data stored in a data
source (usually a relational database).
Table 1: Java JDBC classes and interfaces
16
Steps to connect to the database in java
2.Creating connection
3.Creating statement
4.Executing queries
5.Closing connection
17
Steps to connect to the database in java
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
18
2) Create the connection object
oThe getConnection() method of DriverManager class is used to establish
connection with the database.
Syntax of getConnection() method
Connection con=DriverManager.getConnection("jdbc:mysql://hostname:port/dbName",“un",“PW");
19
Example to connect to the database in java:
public class JDBCDemo {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase",
"username", "password");
if (connection != null) {
System.out.println("Connected to the database!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
} } }}
20
3) Create the Statement object
oThe createStatement() method of Connection interface is used to create
statement. The object of statement is responsible to execute queries with the
database.
21
4) Execute the query
oThe executeQuery() method of Statement interface is used to execute queries
to the database. This method returns the object of ResultSet that can be used to
get all the records of a table.
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
22
SQL (Structured Query Language)
import java.sql.*;
Example : con.close();
JDBC example
24
oPreparedStatement interface
Example1
25
statement = connection.createStatement();
String insertSql = "INSERT INTO customers (name, age)
VALUES (‘abdi jemal’, 20)";
statement.executeUpdate(insertSql);
String updateSql = "UPDATE customers SET age = 21
WHERE name = ‘abdi jemal’";
statement.executeUpdate(updateSql);
String deleteSql = "DELETE FROM customers WHERE name
= ‘abdi jemal'";
statement.executeUpdate(deleteSql);
System.out.println("CRUD operations completed
successfully!");
} catch (Exception e) {
}
26
oResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from
the data. If you have to get metadata of a table like total number of column,
column name, column type etc. , ResultSetMetaData interface is useful
because it provides methods to get metadata from the ResultSet object.
27
oResultSetMetaData Interface
Method Description
public int getColumnCount()throws it returns the total number of columns
SQLException in the ResultSet object.
public String getColumnName(int it returns the column name of the
index)throws SQLException specified column index.
Example1 Example2
28
statement = connection.createStatement();
System.out.println("----------");
29
oDatabaseMetaData interface
DatabaseMetaData interface provides methods to get meta data of a database
such as database product name, database product version, driver name, name
of total number of tables, name of total number of views etc.
30
omethods of DatabaseMetaData interface
public String getDriverName()throws SQLException: it returns the
name of the JDBC driver.
public String getDriverVersion()throws SQLException: it returns the
version number of the JDBC driver.
public String getUserName()throws SQLException: it returns the
username of the database.
public String getDatabaseProductName()throws SQLException: it
returns the product name of the database.
public String getDatabaseProductVersion()throws SQLException: it
returns the product version of the database.
public ResultSet getTables(String catalog, String schemaPattern,
String tableNamePattern, String[] types)throws SQLException: it
returns the description of the tables of the specified catalog. The table
type can be TABLE, VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.
Example
31
// Get DatabaseMetaData object