JDBC Connectivity
JDBC Connectivity
JDBC is an acronym for Java Database Connectivity. It’s an advancement for ODBC ( Open Database
Connectivity ). JDBC is a standard API specification developed in order to move data from the front end
to the back end. This API consists of classes and interfaces written in Java. It basically acts as an interface
(not the one we use in Java) or channel between your Java program and databases i.e it establishes a
link between the two so that a programmer can send data from Java code and store it in the database
for future use.
As previously told JDBC is an advancement for ODBC, ODBC being platform-dependent had a lot of
drawbacks. ODBC API was written in C, C++, Python, and Core Java and as we know above languages
(except Java and some part of Python )are platform-dependent. Therefore to remove dependence, JDBC
was developed by a database vendor which consisted of classes and interfaces written in Java.
Below are the steps that explains how to connect to Database in Java:
Let us discuss these steps in brief before implementing by writing suitable code to illustrate connectivity
steps for JDBC.
In order to begin with, you first need to load the driver or register it before using it in the program.
Registration is to be done once in your program. You can register a driver in one of two ways mentioned
below as follows:
2-A Class.forName()
Here we load the driver’s class file into memory at the runtime. No need of using new or create objects.
The following example uses Class.forName() to load the Oracle driver as shown below as follows:
Class.forName(“oracle.jdbc.driver.OracleDriver”);
2-B DriverManager.registerDriver()
DriverManager is a Java inbuilt class with a static member register. Here we call the constructor of the
driver class at compile time. The following example uses DriverManager.registerDriver()to register the
Oracle driver as shown below:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())
user: Username from which your SQL command prompt can be accessed.
password: password from which the SQL command prompt can be accessed.
Where oracle is the database used, thin is the driver used, @localhost is the IP Address where a
database is stored, 1521 is the port number and xe is the service provider. All 3 parameters above are of
String type and are to be declared by the programmer before calling the function. Use of this can be
referred to form the final code.
Once a connection is established you can interact with the database. The JDBCStatement,
CallableStatement, and PreparedStatement interfaces define the methods that enable you to send SQL
commands and receive data from your database.
Use of JDBC Statement is as follows:
Statement st = con.createStatement();
Now comes the most important part i.e executing the query. The query here is an SQL Query. Now we
know we can have multiple types of queries. Some of them are as follows:
The executeQuery() method of the Statement interface is used to execute queries of retrieving values
from the database. This method returns the object of ResultSet that can be used to get all the records of
a table.
The executeUpdate(sql query) method of the Statement interface is used to execute queries of
updating/inserting.
Pseudo Code:
int m = st.executeUpdate(sql);
if (m==1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
Java
/*
*7. close
*/
import java.io.*;
import java.sql.*;
class GFG {
String url
String query
Class.forName(
System.out.println(
Statement st = con.createStatement();
ResultSet rs
rs.next();
String name
= rs.getString("name"); // Retrieve name from db
System.out.println("Connection Closed....");
Output:
So finally we have sent the data to the specified location and now we are on the verge of completing our
task. By closing the connection, objects of Statement and ResultSet will be closed automatically. The
close() method of the Connection interface is used to close the connection. It is shown below as follows:
con.close();
Example:
Java
import java.sql.*;
import java.util.*;
// Main class
class Main {
// Custom initialization
System.out.println("enter class");
try {
// Registering drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());
pass);
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println(
else
System.out.println("insertion failed");
con.close();
System.err.println(ex);
}
}