Step by Step JDBC Program Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to implement the first JDBC Program example. Here are the different steps to connect and working with the database through Java code.
To implement the JDBC code in Java program, typically we have 6 different steps, are listed below.
Load a JDBC Driver class
Establish a Connection
Create a Statement
Execute Sql queries
Process the ResultSet
Close Connection
To write a simple JDBC program, we should follow these 6 different steps. Lets understand indetail for each step.
Load a JDBC Driver : To load a class at runtime into JVM, we need to call a static method called forName() of java.lang.Class. By calling the forName() method we can load the JDBC Driver class into the JVM.
class.forName("sun.jdbc.odbc.JdbcOdbcDriver);
Establish a Connection : By using the DriverManager class, we can establish the connection to the database. By calling the getConnection(String url, String user,String password) static factory method in DriverManager class, we can obtain a Connection object. To get the connection object we need to proved the connection url as a first parameter and database username and password as second and third parameters. Like below.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetutorialspoint","root","123456");
Create a Statement : In order to send the SQL commands to database from our java program, we need Statement object. We can get the Statement object by calling the createStatement() method on connection.
Statement stmt = con.createStatement();
Execute Sql queries : Inorder to execute the SQL commands on database, Statement interface provides provides three different methods:
executeUpdate()
executeQuery()
execute()
When we want to run the non-select operations then we  can choose executeUpdate()
int count = stmt.executeUpdate("non-select command");
When we want to execute select operations then we  can choose executeQuery()
ResultSet rs = stmt.executeQuery("select command");
When we want to run both select and non-select operations, then we can use execute()
boolean isTrue = stmt.executeQuery("select / non-select command");
Process the ResultSet :  For the select operations, we use executeQuery() method. The executed query returns the data in the form of ResultSet object. To process the data we need to go through the ResultSet.
ResultSet rs = stmt.executeQuery("select * from emp");

while(rs.next()){
      System.out.println(rs.getInt(1));
}
Close Connection : Last but not least, finally we need to close the connection object. It is very important step to close the connection. Else we may get JDBCConnectionException exception.
con.close();
package com.onlinetutorialspoint.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Jdbc_Select_Example {

    public static void main(String[] args) throws Exception {
        // Step - I
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Step - II
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/jdbc", "root", "123456");
        // Step -III
        Statement stmt = con.createStatement();
        // Step - IV
        ResultSet rs = stmt.executeQuery("select * from student");
        // Step - V
        while (rs.next()) {
            System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
                    + rs.getString(3));
        }
        // Step - VI
        rs.close();
        stmt.close();
        con.close();
    }

}
Happy Learning 🙂

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step By Step Spring Boot Docker Deployment Example
Install Mysql on Windows 10 Step by Step
Step by Step – How to push the project into GIT Repository
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step By Step Spring Boot Docker Deployment Example
Install Mysql on Windows 10 Step by Step
Step by Step – How to push the project into GIT Repository
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions