DR - Poorna Chandra S
DR - Poorna Chandra S
January 7, 2023
1 DriverManager class
2 Blob class
Dr.Poorna Chandra S PhD JDBC
JDBC IV
3 Clob class
4 Types class
What is API
API (Application programming interface) is a document that
contains a description of all the features of a product or
software. It represents classes and interfaces that software
programs can follow to communicate with each other. An API
can be created for applications, libraries, operating systems,
etc.
Advantages
easy to use
can be easily connected to any database
Disadvantages
Dr.Poorna Chandra S PhD JDBC
JDBC Driver IV
Advantages
performance upgraded than JDBC-ODBC bridge driver
Disadvanatages
The Native driver needs to be installed on the each
client machine.
Dr.Poorna Chandra S PhD JDBC
JDBC Driver VI
Advantages
No client side library is required because of application
server that can perform many tasks like auditing, load
balancing, logging etc.
Disadvantages
Network support is required on client machine.
Requires database-specific coding to be done in the
middle tier.
Maintenance of Network Protocol driver becomes costly
because it requires database-specific coding to be done
in the middle tier.
4 Thin driver
The 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.
Advantages
7 while ( rs . next () ) {
8 System . out . println ( rs . getInt (1) + " " + rs .
getString (2) ) ;
9 }
Create a Table
Before establishing connection, let’s first create a table in
oracle database. Following is the SQL query to create a table.
create table emp(id number(10),name varchar2(40),age
number(3));
Example
3 Username:
The default username for the oracle database is root
4 Password:
It is the password given by the user at the time of
installing the mysql database. In this example, we are
going to use root as a password
Create a Table
Let’s first create a table in the mysql database, but before
creating table, we need to create database first.
create database sonoo; use sonoo; create table
emp(id int(10),name varchar(40),age int(3));
Example
15 }
16 }
9 ResultSet . CONCUR_UPDATABLE ) ;
10 ResultSet rs = stmt . executeQuery ( " select * from
emp765 " ) ;
11
16 con . close () ;
17
18 } catch ( Exception e ) { System . out . println ( e ) ;}
19
20 }
21 }
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.
Commonly used methods of ResultSetMetaData
interface
17 con . close () ;
18 } catch ( Exception e ) { System . out . println ( e ) ;}
19 }
20 }
Let’s write the jdbc code to store the image in the database.
Here we are using d:
d.jpg for the location of image. You can change it according
to the image location.
Example
15 int i = ps . executeUpdate () ;
16 System . out . println ( i + " records affected " ) ;
17
18 con . close () ;
19 } catch ( Exception e ) { e . printStackTrace () ;}
20 }
21 }
Now let’s write the code to retrieve the image from the
database and write it into the directory so that it can be
displayed.
In AWT, it can be displayed by the Toolkit class. In servlet,
jsp, or html it can be displayed by the img tag. Example
26 }
Let’s
see the table structure of this example to retrieve the file.
In
this example we are going to call the stored procedure
INSERTR that receives id and name as the parameter and
inserts it into the table user420. Note that you need to create
the user420 table as well to run this application.
Dr.Poorna Chandra S PhD JDBC
CallableStatement V
1 import java . sql .*;
2 public class Proc {
3 public static void main ( String [] args ) throws
Exception {
4
5 Class . forName ( " oracle . jdbc . driver . OracleDriver " )
;
6 Connection con = DriverManager . getConnection (
7 " jdbc : oracle : thin : @localhost :1521: xe " ," system " ,"
oracle " ) ;
8
9 CallableS tateme nt stmt = con . prepareCall ( " { call
insertR (? ,?) } " ) ;
10 stmt . setInt (1 ,1011) ;
11 stmt . setString (2 , " Amit " ) ;
12 stmt . execute () ;
13
14 System . out . println ( " success " ) ;
15
16 System . out . println ( stmt . getInt (1) ) ;
17
18 }
19 }
If you see the table emp400, you will see that 2 records has
been added.
54
55 }}
It will ask to add more records until you press n. If you press
n, transaction is committed.
26 ps . setInt (1 , id ) ;
27 ps . setString (2 , name ) ;
28 ps . setInt (3 , salary ) ;
29
30 ps . addBatch () ;
42 con . close () ;
43 } catch ( Exception e ) { System . out . println ( e ) ;}
44
45 }}
1 jdbcRowSet
2 CachedRowSet
3 WebRowSet
4 joinRowSet
5 FilteredRowSet
30 }
31 }
Let’s write the code to retrieve the data and perform some
additional tasks while the cursor is moved, the cursor is
changed, or the rowset is changed. The event handling
operation can’t be performed using ResultSet, so it is preferred
now.
Example
Dr.Poorna Chandra S PhD JDBC
JDBC RowSet VIII
1 import java . sql . Connection ;
2 import java . sql . DriverManager ;
3 import java . sql . ResultSet ;
4 import java . sql . Statement ;
5 import javax . sql . RowSetEvent ;
6 import javax . sql . RowSetListener ;
7 import javax . sql . rowset . JdbcRowSet ;
8 import javax . sql . rowset . RowSetProvider ;
9
10 public class RowSetExample {
11 public static void main ( String [] args )
throws Exception {
12 Class . forName ( " oracle . jdbc .
driver . OracleDriver " ) ;
13
14 // Creating and Executing RowSet
15 JdbcRowSet rowSet = RowSetProvider .
newFactory () . createJdbcRowSet () ;
33 }
34 }
35
36 class MyListener implements RowSetListener {
37 public void cursorMoved ( RowSetEvent event )
{
38 System . out . println ( " Cursor Moved
... " ) ;
39 }
40 public void rowChanged ( RowSetEvent event ) {
41 System . out . println ( " Cursor
Changed ... " ) ;
42 }
43 public void rowSetChanged ( RowSetEvent event
) {
44 System . out . println ( " RowSet
changed ... " ) ;
45 }
46 }