Pages

Thursday 2 January 2020

Introduction

JDBC is a technology which is used to interact with the database from Java Application
JDBC Technology is a part of Java Standard Edition
JDBC is a Specification provided by Java vendor and implemented by Java Vendor or DB vendor.

JDBC Versions
JDBC 3.0 is released under J2SE 1.4.2
No updation under J2SE 5.0
JDBC 4.0 is released under Java SE 6
JDBC 4.1 is released under Java SE 7
JDBC 4.2 is released under Java SE 8

Java Program which is using JDBC API  is called as JDBC Program.
Two packages provided under JDBC API called:
java.sql
javax.sql

Classes and Interfaces under java.sql package
DriverManager
Types
Driver
Connection
Statement
PreparedStatement
CallableStatement
ResultSet
ResultSetMetaData
DatabaseMetaData

Classes and Interfaces under javax.sql package
RowSet
JdbcRowSet
CachedRowSet
DataSource

Types of JDBC Drivers
There are four types of JDBC drivers
Type I Driver:   JDBC ODBC Bridge Driver
Type II Driver:  Partial Java and Partial Native Driver
Type III Driver: Net Protocol Driver
Type IV Driver: Pure Java Driver

Pros and Cons of Types of Drivers


Advantages
Disadvantages
Type I Driver
Type I is very easy to use and maintain.
Type I is suitable for migrating an application to Java without changing existing ODBC setup.
No extra software is required for Type I implementation.
The performance of Type I is acceptable.
Type I driver implementation is possible in window OS only because ODBC drivers are available only with windows.
The performance of this driver is not excellent but acceptable.
Type II Driver
Type II is faster than all other drivers.
In Type II both client and server machine will have the database library.
When the database is migrated then you will get much maintenance because you need to re-install client-side libraries in all the client machines.
Type III Driver
In Type III, client-side DB libraries are moved to the middleware server called the IDS server.
Because of this, client-side maintenance is reduced.
You need to purchase extra software called the IDS server.
Because of having a middleware server between your program and database server, performance will be reduced
Type IV Driver
This driver is best among all the drivers and highly recommendable to use.
Negligible


Note: Type I driver support is removed from Java 8.

Type IV Driver
Name
Pure Java Driver
Vendor
Database Vendor
Username
<Database Username>
Password
<Database Password>
Software Required
Database, Java

For Oracle
Driver Class
oracle.jdbc.driver.OracleDriver
URL
Jdbc:oracle:thin:@<host>:<port>:<serverName>
e.g. jdbc:oracle:thin:@localhost:1521:XE
Classpath
ojdbc14.jar
ojdbc6.jar

For MySQL 
Driver Class
com.mysql.jdbc.Driver
URL
jdbc:mysql://<host>:<port>/<dbName>
e.g. jdbc:mysql://localhost:3306/testdb
Classpath
mysql.jar

Architecture
Type IV Driver

Steps to Write JDBC Program
Step 1: Load the Driver class.
Step 2: Establish the Connection between the JDBC program and Database.
Step 3: Prepare the SQL statement.
Step 4: Create the JDBC statement.
Step 5: Submit the SQL statement to Database using JDBC statement.
Step 6: Process the result.
Step 7: Close all the resources.

Create the below database and table in MySQL
create database testdb;
use testdb;
create table student(sid int primary key,name varchar(20),state varchar(20));

JDBC Program to insert a record into the database(MySQL)
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;

  5. public class JdbcDemo {
  6. public static void main(String[] args) {
  7. Connection con = null;
  8. Statement st = null;
  9. try {
  10. // Step 1:Load the Driver class.
  11. Class.forName("com.mysql.jdbc.Driver");
  12. // Step 2: Establish the connection.
  13. final String url = "jdbc:mysql://localhost:3306/testdb";
  14. con = DriverManager.getConnection(url, "root", "password");
  15. // Step 3: Prepare the SQL statement.
  16. String sql = "insert into student values(101,'Anu','M.P')";
  17. // Step 4: Create JDBC statement
  18. st = con.createStatement();
  19. // Step 5: Submit the SQL statement to Database using JDBC statement.
  20. int x = st.executeUpdate(sql);
  21. // Step 6: Process the result.
  22. if (x == 1) {
  23. System.out.println("Record Inserted");
  24. } else {
  25. System.out.println("Record Not Inserted");
  26. }
  27. } catch (ClassNotFoundException | SQLException e) {
  28. e.printStackTrace();
  29. } finally {
  30. // Step 7: Close all the resources.
  31. try {
  32. if (con != null)
  33. con.close();
  34. if (st != null)
  35. st.close();
  36. } catch (SQLException e) {
  37. System.out.println("Exception occur while closing the resources");
  38. }
  39. }
  40. }
  41. }