Java Database - Connectivity
Java Database - Connectivity
Odbc
SQL
data oracle Mysql
Sever
source
Steps for connection with
the database.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Java Application
JDBC API
Bridge Driver
DSN
ODBC
Driver
Database
Dsn means data source name.
It is use for local connection.
Here jdbc-odbc driver converts all jdbc calls
into ODBC calls and send them .
Then ODBC driver forwards the call to database
server.
In short we can say
Translates query obtained by JDBC into
corresponding ODBC query, which is then handled
by the ODBC driver.
Advantages
Almost any database for which ODBC driver is installed,
can be accessed.
DisAdvantage
The ODBC driver needs to be installed on the client
machine.
Considering the client-side software needed, this
might not be suitable for applets.
Native-API
Java Application
JDBC APT
Native API
Database
TYPE-2 diver known as Native-API
Native-API driver is a database driver
implementation that uses the client-side libraries
of the database.
A native-API partly Java technology-enabled
driver converts JDBC calls into calls on the client
API for Oracle, mysql and other DBMS.
Its communicates directly with database server.
Type 2 drivers use a native API to communicate
with a database system.
Java native methods are used to invoke the
API functions that perform database operations.
Type 2 drivers are generally faster than Type 1
drivers.
Prepared by Akshay Sarvaiya (B.C.A College - Bhacha)
Advantages
Better performance than Type 1 since no jdbc to
odbc translation is needed.
DisAdvantage
The vendor client library needs to be installed on the
client machine.
Cannot be used in internet due the client side
software needed.
It is not suitable for distributed application.
Network-Protocol
Java Application
JDBC APT
Middleware
component
Database
TYPE-3 diver known as Network-Protocol
Its follows three tiered applroach
Here database request passed to a middle-tier
server .
Middle-tier server then translates the request and
passes to specific database native connectivity
interface and also forwards request to database
server.
If middle-tier is written in java that can be use
type-1 and type-2 driver for forward request to the
database.
Advantages
Better performance than Type 1 since no jdbc to
odbc translation is needed.
DisAdvantage
The vendor client library needs to be installed on the
client machine.
Cannot be used in internet due the client side
software needed.
It is not suitable for distributed application.
DELIMITER //
DELIMITER ;
Without parameter
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection
(“jdbc:mysql://localhost:3306/EMP”,”root”,”root”);
CallableStatement stmt =
con.prepareCall("{call proc2}");
stmt.execute();
Stored procedure with two IN
parameter
DELIMITER //
CREATE PROCEDURE
proc3(IN code1 varchar(10),IN name1 varchar(10))
BEGIN
insert into emp values(code1,name1);
END; //
DELIMITER ;
Stored procedure with two
IN parameter
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection
(“jdbc:mysql://localhost:3306/EMP”,”root”,”root”);
CallableStatement stmt =
con.prepareCall("{call proc3(?,?)}");
stmt.setString(1, code);
stmt.setString(2, name);
Stored procedure with one IN and
OUT parameter
DELIMITER //
CREATE PROCEDURE
proc4(IN code1 varchar(10),OUT name1 varchar(10))
BEGIN
SELECT name from emp where code=code1
INTO name1;
END; //
DELIMITER ;
Stored procedure with two
IN parameter
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection
(“jdbc:mysql://localhost:3306/EMP”,”root”,”root”);
CallableStatement stmt =
con.prepareCall("{call proc4(?,?)}");
stmt.setString(1, ”abc”);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(2));
ResultSet interface
The object of ResultSet maintains a cursor pointing to a row of
a table. Initially, cursor points to before the first row.