rajesh jdbc
TRANSCRIPT
![Page 1: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/1.jpg)
Java Database Connectivity
![Page 2: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/2.jpg)
Java Database Connectivity (JDBC)
JDBC – provides an interface to Relational Data Sources.
JDBC library provides the means for executing SQL statements to access and operate on a relational database.
JDBC library is implemented in the java.sql package
Set of classes and interfaces that provide a uniform API for access to broad range of databases.
![Page 3: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/3.jpg)
Java Database Connectivity (JDBC) JDBC:
establishes a connection with a database
sends SQL statements . processes the results.
![Page 4: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/4.jpg)
Talking to Databases
Java Application
JDBC
AccessDatabase
OracleDatabase
SybaseDatabase
![Page 5: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/5.jpg)
JDBC Concepts JDBC’s design is very similar to the design of
ODBC.
Driver Manager Loads database drivers, and manages the
connection between the application and the driver.
Driver Translates API calls into operations for a specific
data source
Connection A session between an application and a
database.
![Page 6: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/6.jpg)
JDBC Concepts (contd.)
Statement An SQL Statement to perform a
query or update operation.
Metadata Information about returned data,
the database and the driver.
ResultSet Logical set of columns and rows
returned by executing an SQL statement (resulting tuples).
![Page 7: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/7.jpg)
JDBC Component Interaction
DriverManager
Connection Statement ResultSet
Driver
Database
Creates Creates Creates
SQL
Result(tuples)
EstablishLink to DB
![Page 8: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/8.jpg)
Two-Tier Database Access Model
Java Application talks directly to the database.
Accomplished through the JDBC driver which sends commands directly to the database.
Results sent back directly to the application.
A JDBC driver sits on the client machine.
e.g. HTTP, email
Application Space
Java Application
JDBC Driver
Database
SQLCommand
ResultSet
![Page 9: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/9.jpg)
Three-Tier Database Access Model
JDBC driver sends commands to a middle tier, which in turn sends commands to database.
middle tier can provide • a higher-level API, not just SQL • control over database access • performance advantages ex. load balancing and caching frequently accessed data
The client machine communicates with the middle tier using such protocols as Hypertext Transfer Protocol (HTTP) or Remote Method Invocation (RMI).
Whereas the middle tier and database communication are governed by a DBMS-proprietary protocol.
Application Space
Java Application
JDBC Driver
Database
SQLCommand
ResultSet
Application Server(middle-tier)
ProprietaryProtocol
![Page 10: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/10.jpg)
Three-Tier Database Access Model
Application Space
Java Application
JDBC Driver
Database
SQLCommand
ResultSet
Application Server(middle-tier)
ProprietaryProtocol
Pros:flexible: can change
one part without affecting otherscan connect to
different databases without
changing codespecialization:
presentation / business logic /
data managementcan cache queriescan implement proxies
and firewalls
Cons:higher complexityhigher maintenancelower network
efficiencymore parts to configure
(and buy)
![Page 11: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/11.jpg)
JDBC Drivers
JDBC API, a purely java-based API.
JDBC Driver Manager which communicates with vendor specific drivers that perform real communication with the database.
Java ApplicationJDBC Driver
ManagerJDBC/
ODBC Bridg
e
Vendor Supplied
JDBC Driver
ODBC Drive
r
![Page 12: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/12.jpg)
Type 1: JDBC-ODBC Bridge, Plus ODBC Driver
JDBC-ODBC bridge plus ODBC driver basically converts JDBC calls to Microsoft’s open database connectivity(ODBC).
As with JDBC , ODBC programming interface that you can use to access most type of relational databases on all type of platform.
Application Space
Java Application
JDBC – ODBC Bridge
Database
SQLCommand
ResultSet
ODBC Driver
ProprietaryProtocol
![Page 13: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/13.jpg)
Type 1: JDBC-ODBC Bridge, Plus ODBC Driver
Advantages:• Can be useful for databases
where other methods not available (e.g., MSAccess).
Disadvantages:• Client computer needs to be
configured with ODBC driver and ODBC instance which specifies database server and other information.
• JDBC performance relies on ODBC driver.
![Page 14: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/14.jpg)
JDBC DataTypes
SQL Type Java TypeCHAR StringVARCHAR StringLONGVARCHAR StringNUMERIC
java.Math.BigDecimalDECIMAL
java.Math.BigDecimalBIT booleanTINYINT intSMALLINT intINTEGER intBIGINT longREAL floatFLOAT doubleDOUBLE doubleBINARY byte[]VARBINARY byte[]DATE
java.sql.DateTIME
java.sql.TimeTIMESTAMP
java.sql.Timestamp
![Page 15: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/15.jpg)
Steps during execution
1. Importing Packages .2. Registering the JDBC Drivers .3. Opening a Connection to a Database
.4. Creating a Statement Object. 5. Executing a Query and Returning a
Result Set Object .6. Processing the Result Set .7. Closing the Result Set and
Statement Objects .8. Closing the Connection.
![Page 16: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/16.jpg)
1. Importing Packages
// Program name: jdbc_smvdu.java
//Import packages
import java.sql.*; // JDBC packages import java.math.*; import java.io.*;import oracle.jdbc.driver.*;
![Page 17: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/17.jpg)
2. Load JDBC Drivers
class jdbc_smvdu {
public static void main (String args []) throws SQLException {
// Load Odbc driver
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
![Page 18: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/18.jpg)
3. Opening Connection to a Database
//Prompt user for username and password String user; String password;
user = readEntry
("username: "); password = readEntry
("password: ");
// Connect to the local database
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@aardvark:1526:teach", user, password);
![Page 19: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/19.jpg)
4. Creating a Statement
Statement stmnt= conn.createStatement();
String st= “Select first name, age from Employee where id= 20 ”;
![Page 20: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/20.jpg)
5. Executing a Query, Returning a ResultSet Object.6. Processing the ResultSet.
ResultSet rset = stmnt.executeQuery (s);
// Print query results
while (rset.next ()) System.out.println (rset.getString (1)+" "+ rset.getString(2));
![Page 21: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/21.jpg)
7. Close the ResultSet and Statement Objects.8. Closing the Connection.
// close the result set, statement, and the connection
rset.close( ); stmnt.close( ); conn.close( );
}
![Page 22: Rajesh jdbc](https://reader031.vdocuments.site/reader031/viewer/2022013111/554a3078b4c90526578b52a3/html5/thumbnails/22.jpg)