JDBC : Java Database Connectivity

Niraj Bhoi
4 min readMay 29, 2021

--

Java is the popular language for developing android application and also in the field of web development.For every application there is a need of a secured database. The database is the most important part of any application. We can create a database easily but the main thing is how we can connect it to the our application?

For connecting the database , very first method introduced is ODBC. ODBC stands for Open Database Connectivity. It allows the application to access the data in database management system using SQL. It is provided by Microsoft. The main problem with this is that , it is platform dependent and unsecured. To solve this issue JAVA created its own API i.e. JDBC Api , which uses JDBC drivers written in java language. For less number of records in database the ODBC is best but as the number of records increases the JDBC came out as vanquisher.

JDBC drivers

The description of all features of the product is stored in one document known as Application Programming Interface i.e. API. It represents the classes and interfaces. The jdbc api uses jdbc drivers to connect the application to the database. It has four types of drivers

  1. JDBC-ODBC Bridge Driver :- It is easily connected to any database. It converts jdbc function calls into function call of odbc. Because of this reason the performance of this driver is degraded.
  2. Native API Driver :- It uses the client side libraries of the database. It converts the jdbc calls into native calls of the database api. Due to this , the performance of driver becomes better than the jdbc-odbc bridge driver. The main drawback of this , it is installed on the client machine which is quite time consuming.
  3. Network Protocol Driver:- The amazing thing about this driver is that it is fully written in java. This driver converts all calls into a vendor- specific database protocol directly or indirectly. To install this driver on client machine it requires the network support.
  4. Thin driver:- It directly converts calls into vendor specific database protocol. Due to this , this driver is called as thin driver. This drivers is totally depend on the driver. The performance of this driver is better than other drivers.

Connect JAVA application to database using JDBC

  1. Register the driver class :- For registering the driver , we load the driver class dynamically. For that we use forName() method,

2.Creating connection :-For creating connection we use getConnection() method,

3.Creating the statement object :-The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to executing the queries with database.

4.Executing queries:-For executing queries to the database, executeQuery() method is used. It returns object of ResultSet which can be used to get the records of a table.

5.Closing the connection :- By closing the connection between application and database object statement and ResultSet will be closed automatically.

lets take an example, of connecting java database with MySQL

Step 1:- driver class

For My Sql the driver class is com.mysql.jdbc.Driver

Step 2:- Crate connection

The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo .In My Sql is the database and jdbc is Api. The swerver name is the localhost on which My Sql is running, we can also use IP adress instead of this. The next is the name of the database in this example this is “sonoo”.

step 3:- Creating username

The default username of My Sql is root. we can also change it.

Step 4:- Giving Password

The password is must have to give at the time of installation of My Sql. In this example we are using root as a password.

with the help of this code we are fetching the records. For connecting the database with application we have to load the jar file. The jar file contains all java classes. There are two ways of loading the jar file . The first one is by pasting mysqlconnector.jar file in jre/lib/ext in a folder. Another way of doing this is by setting the class path.

Jdbc is the vast topic it also contains various interfaces like driver interface,statement interface,ResultSet interface,etc. JDBC is basically an application programming inter phase for the java programming language to determine the clients database. The coding is the initial step of programming complex queries in JDBC ,which is easier than programming can be executed on all platforms.

Thanks for reading…!

--

--

Responses (3)