Top Banner
JDBC
23

JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

JDBC

Page 2: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• JDBC stands for Java Data Base Connectivity.• JDBC is different from ODBC in that

– JDBC is written in Java (hence is platform independent, object oriented robust etc.), while– ODBC is written in C, is not object oriented.

• However, both JDBC and ODBC are based on the X/Open SQL Command Level Interface (CLI).

• Sun provides a JDBC ODBC bridge, which enables one to connect painlessly to ODBC data sources.

Page 3: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

JDBC /ODBC Bridge

• We use MS Access, a 32 bit ODBC driver.

• First create an access database file e.g. filename.mdb.

• Create a 32-bit ODBC driver via which JAVA will communicate with the database tables.

Page 4: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Steps to create 32-bit Driver

• Select the ODBC data source from control panel; ODBC administrator interface will open up.

• This allows the creation of several types of Data Source Name (DSN).

• Select System DSN.• Add Microsoft Access Driver.• Name your DSN.• Select the database.

Page 5: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• DSN is aware of the existence of MS Access Database.

• Java cant communicate directly with ODBC driver as this driver has been created using different techniques other than Java.

• Hence, using appropriate java code and associated Java driver’s a bridge must be set up between 32-bit ODBC driver and JAVA UI.

• This is a JDBC:ODBC bridge.

Page 6: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Java Application UI32-bit ODBC

Access Driver

Database and

Tables

Java Driver ManagerResponsible for setting up

JDBC:ODBC bridge

Java Connectivity Model

Page 7: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• Java application communicates with a driver manager using java code.

• The DriverManager is a class that belongs to Java.sql package.

• The driver manager communicates with 32-bit ODBC driver.

• Then 32-bit ODBC driver communicates with Access database.

Page 8: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

JavaDriver Manager

ODBC System DSN

Database and Tables

Creates a Connection Object which communicates with ODBC driver and in turns spawns a Result Set object

to hold the record set returned by the query made to database table.

The ResultSet Object holds the recoreds retrieved from the database table. Data from this result set object used in java application.

Page 9: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Driver Manager

• Java’s Driver Manager controls interaction between UI and database.

• It can support multiple drivers to different DBMS systems.

• It loads the requested driver and provides support for managing database connections.

Page 10: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• It can perform-

• Locate a driver for a particular database.

• Process JDBC initialization calls.

• Provides entry point to JDBC functions for each specific driver.

• Performs validation for JDBC calls.

Page 11: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• Once an ODBC driver has been created and registered in Windows registry on the computer, the java DriverManager class can be used to invoke this driver.

• The DriverManager class works as the interface between UI and ODBC driver.

Page 12: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• The DriverManager provides method to establish a connection to specified database-

• getConnection(JDBC:ODBC:DSN Name,<userName>,<pwd>)• On success it spawns a Connection object which represent a

physical connection to the database.• After physical connection establishment, initialize the SQL queries.• Recoreds retrieved from database which will be held in ResultSet

object.• To see the data held within this object its each row must be read.• next() in loop fetches one row at a time. It returns false when there

are no more rows in ResultSet object.

Page 13: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Java.sql package

• DriverManager class is in java.sql package.• getConnection(URL,user,pwd) establish a connection.

On success, it returns a Connection object.• A connection object represents a connection with a

database.• Methods used with this objects are-• void close() to release database resources.• Statement CreateStatement() returns a new

statement object that can be used to execute SQL statements.

• On error both throws SQLException.

Page 14: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

Statement Interface

• Statement object execute SQL statements that are static and obtain results in ResultSet object.

• Some methods are-• ResultSet executeQuery(String SQLquery)

– executes a SQL statement that returns a single ResultSet object. On error SQLException is thrown.

• int executeUpdate(String SQLquery)– It executes SQL UPDATE, DELETE, or INSERT

statement. A row count is returned by these statement. Zero returned if no row is returned. On error SQLException is thrown.

Page 15: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

PreparedStatement Interface

• It extends Statement interface.• If an application requires multiple executions of any SQL

statement, it is pre-compiled and stored in a PreparedStatement object. This object can then be used by many times to execute the statement.

• Some methods are-• executeQuery()• executeUpdate()

– SQL statements that don’t return anything such as DDL can also be executed.

• On Error SQLException is thrown

Page 16: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

ResultSet Interface

• ResultSet object watches a single row of data at a time.• This row of data is called its current row.• When a SQL statement is re-executed the current

ResultSet is closed and a new ResultSet object is created.

• Some methods are-• void close()• void getString()

– Gets the value of a column in the current row as a Java String.• next()

– A ResultSet is initially positioned before its first row. The first call to next() makes the first row the current row. The second call makes the second row the current row, etc.

Page 17: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

1. Choosing an appropriate driver

• For every specific database connection established, an appropriate driver is required to allow JDBC API to manage the connection.

• Java program code is used to force a particular driver to be loaded.

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Page 18: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

2. Create a connection

• getConnection() of DriverManager class is used.

• String url = “jdbc:odbc:DSNName”;

• Connection conn = DriverManager.getConnection(url);

Page 19: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

3. Perform database query

• A connection object needs to be used to send SQL statement to database.

• After creating the connection, a SQL statement has to be created.

• Statement stmt = conn.createStatement();• The SQL statement will be sent to DBMS where

it is optimized and executed.• ResultSet rs = stmt.executeQery(“Select * From

Emp”);• int rows =stmt.executeUpdate(“Update Emp Set

Ename=“Pk” Where Ecode=‘101’”);

Page 20: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• The current row’s content are accessible via getXXX() methods that allow extraction of values from current row in ResultSet object.

• Accessing columns of current row is possible in 2 ways-

• By index number

• By name

Page 21: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

By index

• String getString(int columnIndex);• Boolean getBoolean(int columnIndex);• byte getByte(int columnIndex);• short getShort(int columnIndex);• int getInt(int columnIndex);• long getLong(int columnIndex);• Float getFloat(int columnIndex);• Double getDouble(int columnIndex);

Page 22: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

By Name

• String getString(String columnName);• Boolean getBoolean(String columnName);• byte getByte(String columnName);• short getShort(String columnName);• int getInt(String columnName);• long getLong(String columnName);• Float getFloat(String columnName);• Double getDouble(String columnName);

Page 23: JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.

• Example

• Example

• Example