JDBC – Java Database Connectivity
Java Database Connectivity (JDBC) is an application programming interface (API) for the
programming language Java, which defines how a client may access a database. It is a Javabased
data access technology used for Java database connectivity. It is part of the Java Standard Edition
platform.
The JVM uses a JDBC driver to translate generalized JDBC calls into vendor specific database calls
JDBC API uses JDBC drivers to connect with the database. There are four types of JDBC drivers:
o JDBC-ODBC Bridge Driver,
o Native Driver,
o Network Protocol Driver, and o Thin Driver
JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC
bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged
because of thin driver.
Advantages:
• easy to use.
• can be easily connected to any database.
Disadvantages:
• Performance degraded because JDBC method call is converted into the ODBC function calls.
• The ODBC driver needs to be installed on the client machine.
Native-API 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.
Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why
it is known as thin driver. It is fully written in Java language.
Advantage:
• Better performance than all other drivers.
• No software is required at client side or server side.
Disadvantage:
• Drivers depend on the Database.
Steps to connect to a Database
1. Import JDBC packages.
import java.sql.*;
2. Load and register the JDBC driver.
This is for establishing a communication between the JDBC program and the database.
This is done by using the static registerDriver() method of the DriverManager class of the
JDBC API.
The forName() method of Class class is used to register the driver class. This method is used
to dynamically load the driver class.
The following example uses Class.forName() to load the Oracle driver –
Class.forName(“oracle.jdbc.driver.OracleDriver”);
3. Open a connection to the database.
The getConnection() method of DriverManager class is used to establish connection with
the database.
Syntax of getConnection() method
• public static Connection getConnection(String url)throws SQLException
• public static Connection getConnection(String url,String name,String password)
throws SQLException
Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
4. Create a statement object to perform a query.
5. Execute the statement object and return a query resultset.
Class.forName("net.ucanaccess.jdbc.Ucanacc
essDriver");
Connection c=DriverManager.getConnection("jdbc:ucanaccess://C \\Database2.accdb");
System.out.println("Database Connected");
Statement s=c.createStatement();
s.execute("insert into student values(2,'Chitra','18045','Female','Bcom','II')");
System.out.println("Inserted susccessfully"); c.close(); }
catch(Exception e){
System.out.println(e);
}
}
}
Example 2
//Display records using statement object
import java.sql.*; public class displaydb
{
public static void main(String[] args) {
try{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection c=DriverManager.getConnection("jdbc:ucanaccess://C \\Database2.accdb");
System.out.println("Database Connected");
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("select * from student");
System.out.println("Database Records");
while(rs.next()){
System.out.println(rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getString(5));
}
}
catch(Exception e){
System.out.println(e);
}
}
}
Prepared Statement
The PreparedStatement interface extends the Statement interface. It represents a precompiled
SQL statement which can be executed multiple times. This accepts parameterized SQL quires and
you can pass 0 or more parameters to this query.
Let's see the example of parameterized query:
String sql="insert into emp values(?,?,?)";
As you can see, we are passing parameter (?) for the values. Its value will be set by calling the setter
methods of PreparedStatement.
Initially, this statement uses place holders “?” instead of parameters, later on, you can pass
arguments to these dynamically using the setXXX() methods of the
PreparedStatement interface. Where XXX represents the data type you have used to declare in
database – setInt, setFloat, setDouble, setString.
String sql = "select * from people";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet result = statement.executeQuery();
For methods of ResultSet refer to https://www.journaldev.com/34720/java-resultset