You are on page 1of 5

*************************************************************************

JDBC - JAVA DATABASE CONNECTIVITY


*************************************************************************

➢ JDBC : Java Database Connectivity


➢ JDBC is a Technology which is used to connect java application
with database.
➢ JDBC is an API which provides some classes and interfaces by
which we can work with databases.
➢ JDBC API has Packages to work with databases.
1. java.sql

SQL

I C
1) Driver 1) DriverManager
2) Connection 2) Date
3) Statement 3) Time
4) PreparedStatement
5) ResultSet

Java By Kiran, 4th Floor, Park Plaza, Above Birla Super Market, Near State Bank nagar,Karve
Nagar Pune, Maharashtra. Area Pin-Code 411052, +918888809416
Q-> What are the general/common steps to communicate with database?
1) Check the MySQL version

2) Download MySQL connector jar file from below link.


https://mvnrepository.com/artifact/mysql/mysql-connector-java

3) Create Java Application and load MySQL jar file.


- Right click on Java Project Folder .
- Click on Build Path.
- Click on Configure Build Path.
- Click on Libraries then click on Classpath and then Add
External JARs

Java By Kiran, 4th Floor, Park Plaza, Above Birla Super Market, Near State Bank nagar,Karve
Nagar Pune, Maharashtra. Area Pin-Code 411052, +918888809416
****************************************************************

Java Database Connectivity with 5 Steps


****************************************************************

1) Load and Register Driver class


2) Create connection
3) Create statement
4) Execute queries
5) Close connection

1) Register the driver class

-> The forName() method of Class class is used to register the driver class.
This method is used to dynamically load the driver class.

2) Create the connection object


-> It helps to establish a connection between java application and database.

-> The getConnection() method of DriverManager class is used to establish


connection with the database.

-> Commonly used method.

1) public Statement createStatement();

->creates a statement object that can be used to execute SQL queries.

Java By Kiran, 4th Floor, Park Plaza, Above Birla Super Market, Near State Bank nagar,Karve
Nagar Pune, Maharashtra. Area Pin-Code 411052, +918888809416
3) Create the Statement object
-> The Statement interface provides methods to execute queries with the
database.

-> The createStatement() method of Connection interface is used to create statement.


The object of statement is responsible to execute queries with the database.

-> Commonly used method.

1) public ResultSet executeQuery(String sql);

->is used to execute SELECT query. It returns the object of ResultSet.

2) public int executeUpdate(String sql);

->is used to execute specified query, it may be create, drop, insert, update, delete etc.

4) Execute the query

-> The object of ResultSet maintains a cursor pointing to a row of a table.


-> Initially, cursor points to before the first row.
-> The executeQuery() method of Statement interface is used to execute queries
to the database.This method returns the object of ResultSet that can be used to get all the
records of a table.

-> Commonly used method.

1) public boolean next();


-> is used to move the cursor to the one row next from the current
position.

Java By Kiran, 4th Floor, Park Plaza, Above Birla Super Market, Near State Bank nagar,Karve
Nagar Pune, Maharashtra. Area Pin-Code 411052, +918888809416
2) public int getInt(int columnIndex);
-> is used to return the data of specified column index of the current row
as int.

3) public int getInt(String columnName);


-> is used to return the data of specified column name of the current row
as int.

4) public String getString(int columnIndex);


-> is used to return the data of specified column index of the current row
as String.

5) public String getString(String columnName) ;


-> is used to return the data of specified column name of the current row
as String

5) Close the connection object

->. The close() method of Connection interface is used to close the connection.

Java By Kiran, 4th Floor, Park Plaza, Above Birla Super Market, Near State Bank nagar,Karve
Nagar Pune, Maharashtra. Area Pin-Code 411052, +918888809416

You might also like