JDBC (Java Database connectivity) API defines classes and interfaces for interactions with a relational database and provides capability of writing database based applications in Java. Using the api, you can issue SQL statements to almost all relational databases. It provides access by allowing you to embed SQL statements in Java code. You will get to learn how to load a database driver, create a connection and issue a SQL statement.
The JDBC Architecture
Java Application –> JDBC –> Driver –> Relational Database
Java Application calls JDBC api. JDBC loads a driver that talks to the relational database. This allows us to change database engines without changing the code as such by changing the interacting driver information.
Basics
Before you start writing the class definition, import java.sql package.
import java.sql.*; (* denotes that all classes of java.sql package will get imported).
1. Loading the driver
In this step, you will load the driver class by calling Class.forName() with the driver name as an argument passed. The driver class creates an instance of itself and a client can connect to the database server through a JDBC driver. We will use a ODBC driver (JDBC-ODBC bridge) for connecting to a database server. Class.forName returns a class.
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
This will throw an exception that can be caught by using a try/catch statement or by adding
throws ClassNotFoundException
2. Creating a JDBC connection
The JDBC DriverManager class creates objects that can connect a Java application to a JDBC Driver. This class provides the backbone of the JDBC architecture by managing various drivers installed on the operating system. The get.connection() method is used to obtain a connection to the database server. The arguments passed are username, password and a JDBC URL to obtain a connection to the database. The method returns a Connection object. This object represents a session with the database. SQL statements can be executed only when the connection is obtained. An application can obtain one or more connections to a single database, or it can obtain multiple connections to many databases.
JDBC URL — Each driver has a different syntax for source. For a ODBC driver the syntax is
jdbc:odbc:systemDSNName
A system DSN can be created by going to Control Panel -> Administrative Tools -> Data Sources (ODBC). Provide information to your remote database server and test the connection after creation.
The syntax for connection creation is
String jdbcUrl = “jdbc:odbc:systemDSNName”;
String userName = “scott”;
String password = “tiger”;
Connection dbConnection=DriverManager.getConnection(jdbcUrl,userName,password);
Now that we have obtained a connection, we can issue SQL statements to the database and obtain/manipulate the results.
Summary till now
The following class returns a ODBC connection to a database.
public class DBConnection {
public static Connection getOracleJDBCConnection(String dsn, String userName, String password ) throws ClassNotFoundException, SQLException{
dsn = “jdbc:odbc:”+dsn;
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
return DriverManager.getConnection(dsn, userName, password);
}
}
An object of the above class(inside our main function) is created as
Connection con = DBConnection.getOracleJDBCConnection(“systemDSN”,“scott”,“tiger”);
3. Creating a Statement object
Once a connection is created, your application can interact with the database. The connection interface defines method for interacting with database after initialization of a connection. To execute, you need to create a Statement object from your connection object by
stmt = con.createStatement();
A statement object is used to execute SQL statements to a database and receive data in form of a ResultSet.
4. Issuing a SQL statement to the database
The statement interface defines methods to interact with databases by executing SQL statements. There are three methods for executing statements.
a. executeQuery() - used for a SELECT statement.
b. executeUpdate() — used to create or modify tables.
c. execute() — executes an SQL statement that is written as string object.
The following command creates a sql statement, executes it and returns the data in a ResultSet.
stmt = con.createStatement();
String query = “SELECT * FROM EMPLOYEES”;
ResultSet result = stmt.executeQuery(query);
Similarly other SQL commands (INSERT, UPDATE, ALTER, etc) can be executed.
ResultSet provides interface to a table of data generated after execution of the SQL statement. The table rows are accessed in a sequence. The ResultSet maintains an pointer pointing to the current row of data. The function next() is used to iterate through the rows of the tabular data.
Tags:
Databases,
Java,
JDBC,
ODBC