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
Note: Type I driver support is removed from Java 8.
Type IV Driver
For Oracle
For MySQL
Architecture
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)
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
|
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)
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JdbcDemo {
- public static void main(String[] args) {
- Connection con = null;
- Statement st = null;
- try {
- // Step 1:Load the Driver class.
- Class.forName("com.mysql.jdbc.Driver");
- // Step 2: Establish the connection.
- final String url = "jdbc:mysql://localhost:3306/testdb";
- con = DriverManager.getConnection(url, "root", "password");
- // Step 3: Prepare the SQL statement.
- String sql = "insert into student values(101,'Anu','M.P')";
- // Step 4: Create JDBC statement
- st = con.createStatement();
- // Step 5: Submit the SQL statement to Database using JDBC statement.
- int x = st.executeUpdate(sql);
- // Step 6: Process the result.
- if (x == 1) {
- System.out.println("Record Inserted");
- } else {
- System.out.println("Record Not Inserted");
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- } finally {
- // Step 7: Close all the resources.
- try {
- if (con != null)
- con.close();
- if (st != null)
- st.close();
- } catch (SQLException e) {
- System.out.println("Exception occur while closing the resources");
- }
- }
- }
- }