AJava GTU Study Material Presentations Unit-2 17032021071519AM
AJava GTU Study Material Presentations Unit-2 17032021071519AM
GTU #3160707
Unit-2
JDBC Programming
Reference Book:
Complete Reference J2EE by James Keogh mcgraw publication
Example
Oracle
MS Access
My SQL
SQL Server
..
.
What is API ?
Application Program Interface
A set of routines, protocols, and tools for building software applications.
JDBC is an API, which is used in java programming for interacting with database.
JDBC API
JAVA
JDBC Driver
Application Database
Java Application
DB Vendor
Application Code Driver
Type 1
JDBC ODBC Bridge ODBC Driver Local
DBMS
Database Server
Local Computer
Java Application
DB Vendor Driver
Application Code
Type 2
Native API Local
DBMS
Database Server
Prof. Jayesh D. Vagadiya # 3160707 Unit 2 – JDBC Programming 16
JDBC Driver: Type 2 (Native Code Driver)
Type 3
JDBC-Net pure Java JDBC Type 4 Driver
Database Server
Java Application
Application Code
Type 4
100% Pure Java Local
DBMS
Database Server
DriverManager
drivers. It keeps track of the drivers that
are available and handles establishing a
This interface
connection handlesathe
between communications
database and the
Driver
with the database
appropriate driver. server. Driver interface
provides vendor-specific implementations
Connection of the
This abstract
Interface classes
is the provided
session by java
between the
Interface
SQLException This
data.class handles any errors that occur in a
database application.
java.sql
Class.forName("com.mysql.jdbc.Driver”);
Statement st=con.createStatement();
Syntax:
ResultSet rs=st.executeQuery(“query”);
It holds data retrieved from a
database after you execute an Returns a ResultSet object. Use this
SQL query using Statement method when you expect to get a
objects. It acts as an iterator to result set, as you would with a
allow you to move through its SELECT statement.
data.
Prof. Jayesh D. Vagadiya # 3160707 Unit 2 – JDBC Programming 37
Step 3: Executing Statement
Example :
ResultSet rs = stmt.executeQuery("SELECT * from diet");
Database
ResultSet rs
Enr_no Name Branch
601 abc ce
602 pqr me
603 rst ec
604 def Ci
ResultSet rs
Enr_no Name Branch
Database
601 abc ce
602 pqr me
rs.close();
immediately
Parameter 3
Example Parameter 1
Table Name
Parameter 2
public void setFloat(int paramIndex, float value) Sets the float value to the given parameter index.
public void setDouble(int paramIndex, double value) Sets the double value to the given parameter index.
public int executeUpdate() Executes the query. It is used for create, drop, insert, update, delete etc.
public ResultSet executeQuery() Executes the select query. It returns an instance of ResultSet.
Parameter Description
IN A parameter whose value is unknown when the SQL statement is created. You bind values to IN
parameters with the setXXX() methods.
OUT A parameter whose value is supplied by the SQL statement it returns. You retrieve values from the
OUT parameters with the getXXX() methods.
INOUT A parameter that provides both input and output values. You bind variables with the setXXX()
methods and retrieve values with the getXXX() methods.
3. Update methods Used to update the data in the columns of the current row. The updates can
then be updated in the underlying database as well.
boolean previous() Moves the cursor to the previous row. This method returns false if the
throws SQLException previous row is off the result set.
boolean absolute(int row) throws SQLException Moves the cursor to the specified row.
boolean relative(int row) throws SQLException Moves the cursor the given number of rows forward or backward, from
where it is currently pointing.
int getRow() throws SQLException Returns the row number that the cursor is pointing to.
int getInt(String columnName) throws SQLException Returns the int in the current row in the column named columnName.
int getInt(int columnIndex) throws SQLException Returns the int in the current row in the specified column index. The
column index starts at 1, meaning the first column of a row is 1, the
second column of a row is 2, and so on.
String getString(String columnLabel) Retrieves the value of the designated column in the current row of
throws SQLException this ResultSet object as a String in the Java programming language.
String getString(int columnIndex) Retrieves the value of the designated column in the current row of
throws SQLException this ResultSet object as a String in the Java programming language.
void updateInt(int col_Index, int x) throws SQLException Updates the designated column with an int value.
void updateFloat(int col_Index, float x) throws SQLException Updates the designated column with a float value.
void updateDouble(int col_Index,double x) Updates the designated column with a double value.
throws SQLException
ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the result set.
ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can scroll forward and backward, and the result set is not sensitive to
changes made by others to the database that occur after the result set was created.
ResultSet.TYPE_SCROLL_SENSITIVE The cursor can scroll forward and backward, and the result set is sensitive to changes
made by others to the database that occur after the result set was created.
String getColumnName(int index) throws SQLException it returns the column name of the specified column index.
String getColumnTypeName(int index) throws SQLException it returns the column type name for the specified index.
DabaseInfo.java
1 Class.forName("com.mysql.jdbc.Driver");
2 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college",
3 "root","");
4 DatabaseMetaData dbmd=con.getMetaData();
5 System.out.println("getTransactionIsolation="+con.getTransactionIsolation());
6 System.out.println("getDatabaseProductName:" +dbmd.getDatabaseProductName());
7 System.out.println("getDatabaseProductVersion():"+dbmd.getDatabaseProductVersion());
8 System.out.println("getDriverName():"+dbmd.getDriverName());
9 System.out.println("getDriverVersion():"+dbmd.getDriverVersion());
10 System.out.println("getURL():"+dbmd.getURL());
11 System.out.println("getUserName():"+dbmd.getUserName());
Transaction Succeed
Transaction
Initial State
Transaction Failed
Variable
Undefined
Same query had
retrieved two
different value
One can get/set the current isolation level by using methods of Connection interface:
1. getTransactionIsolation()
2. setTransactionIsolation(int isolationlevelconstant)
java.sql. It provides the update counts for all commands that were executed successfully
BatchUpdateException during the batch update.