The following 5 steps are the basic steps involve in connecting a Java application with Database using JDBC.
It is first an essential part to create JDBC connection. JDBC API provides a method Class.forName()
which is used to load the driver class explicitly. For example, if we want to load a jdbc-odbc driver then the we call it like following.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
After registering and loading the driver in step1, now we will create a connection using getConnection()
method of DriverManager class. This method has several overloaded methods that can be used based on the requirement. Basically it require the database name, username and password to establish connection. Syntax of this method is given below.
getConnection(String url)
getConnection(String url, String username, String password)
getConnection(String url, Properties info)
This is a sample example to establish connection with Oracle Driver
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","username","password");
import java.sql.*;
class Test {
public static void main(String[] args) {
try {
//Loading driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//creating connection
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");
Statement s = con.createStatement(); //creating statement
ResultSet rs = s.executeQuery("select * from Student"); //executing statement
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close(); //closing connection
} catch (Exception e) {
e.printStacktrace();
}
}
}
In this step we will create statement object using createStatement() method. It is used to execute the sql queries and defined in Connection class. Syntax of the method is given below.
public Statement createStatement() throws SQLException
Statement s=con.createStatement();
After creating statement, now execute using executeQuery()
method of Statement interface. This method is used to execute SQL statements. Syntax of the method is given below.
public ResultSet executeQuery(String query) throws SQLException
In this example, we are executing a sql query to select all the records from the user table and stored into resultset that further is used to display the records.
ResultSet rs=s.executeQuery("select * from user");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
This is final step which includes closing all the connection that we opened in our previous steps. After executing SQL statement you need to close the connection and release the session. The close()
method of Connection interface is used to close the connection.
public void close() throws SQLException
con.close();
Now lets combine all these steps into a single example and create a complete example of JDBC connectivity.
import java.sql.*;
class Test {
public static void main(String[] args) {
try {
//Loading driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//creating connection
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");
Statement s = con.createStatement(); //creating statement
ResultSet rs = s.executeQuery("select * from Student"); //executing statement
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close(); //closing connection
} catch (Exception e) {
e.printStacktrace();
}
}
}