Sei sulla pagina 1di 12

JDBC

- Pritam Chatterjee
JDBC
• Java JDBC is a java API to connect and execute query
with the database. JDBC API uses jdbc drivers to connect
with the database.

• Java application programming interface (API) is a list


of all classes that are part of the Java development kit
(JDK).
JDBC Drivers
There are 4 types of JDBC drivers:
• Type 1: JDBC-ODBC bridge driver
 JDBC-ODBC bridge driver converts JDBC method calls into the ODBC
function calls.
• Type 2: Native-API driver (partially java driver)
 The Native API driver uses the client-side libraries of the database. The
driver converts JDBC method calls into native calls of the database API. It
is not written entirely in java.
• Type 3: Network Protocol driver (fully java driver)
 The Network Protocol driver uses middleware (application server)
that converts JDBC calls directly or indirectly into the vendor-
specific database protocol. It is fully written in java.
• Type 4: Thin driver (fully java driver)
 The thin driver converts JDBC calls directly into the vendor-specific
database protocol.
Example with Thin Driver

• For connecting with SQL Server: mssql-jdbc-8.2.2.jre8.jar


MySQL: mysql-connector-java-8.0.12.jar
Oracle: ojdbc7.jar

• Steps:
1. Register the driver class
2. Creating connection
3. Creating statement
4. Executing queries
5. Catching exceptions and Closing connection
Database
• Database name: Testing
• Table: Student
• Fields: Name & ID
Step1 and Step 2
To catch exceptions the steps will be in try
block
1. Register the Driver class
• The forName() method of Class class is used to register the
driver class. 
• Class.forName("com.microsoft.sqlserver.jdbc.SQLServerD
river");
• For MySQL: com.mysql.cj.jdbc.Driver

2. Create the connection object


• con =
DriverManager.getConnection("jdbc:sqlserver://PRITAM-
PC:1433;" +
"databaseName=Testing;user=pritam;password=1234");
• In MySQL: con = DriverManager.getConnection(  
• "jdbc:mysql://localhost:3306/Testing",“root",“1234");  
Step1 and Step 2
1. Register the Driver class for Oracle
• Class.forName("oracle.jdbc.driver.OracleDriver");  

• Create the connection object(Oracle)

• Connection con=DriverManager.getConnection(  
• "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 
Step 3 and Step 4
Once a connection is obtained we can interact with the database
using Statement Interfaces.
The JDBC Statement,
CallableStatement, and PreparedStatement interfaces define the
methods and properties that enable you to send SQL commands and
retrieve data from the database.
Statement: general-purpose access to database, static SQL
statements without parameters.
PreparedStatement: accept input parameters at runtime
CallableStatement: access the database stored procedures,
parameterized stored procedure
Step 3 and Step 4
3. Create Statements
 String query = "insert into Students(Name,ID)
values(?,?)";
 prSt = con.prepareStatement(query);
 prSt.setString(1, "Andrew");
 prSt.setString(2, "800");
4. Execute Queries
 int count = prSt.executeUpdate();
Step 3 and Step 4
The execute() method: This method is used to execute SQL
DDL statements, it returns a boolean value specifying
weather the ResultSet object can be retrieved.
executeUpdate(): This method is used to execute
statements such as insert, update, delete. It returns an
integer value representing the number of rows affected.
executeQuery(): This method is used to execute statements
that returns tabular data (example select). It returns an
object of the class ResultSet.
Step 5
• Catching Exceptions
 catch (ClassNotFoundException e) {
 e.printStackTrace(); //
 }
 catch (SQLException e) {
 e.printStackTrace();
 }

• Closing Connection
 finally{
 try{
 if(prSt != null) prSt.close();
 if(con != null) con.close();
 } catch(Exception ex){}
 } }

Potrebbero piacerti anche