Spring JDBC Example (original) (raw)

Last Updated : 4 May, 2026

Spring JDBC provides a simplified approach to interact with relational databases by reducing boilerplate JDBC code. It uses JdbcTemplate to handle common tasks like connection management, query execution, and exception handling efficiently.

**Prerequisite:

Step-by-Step Implementation

Step 1: Create Java Project

Step 2: Setup Database

Data Inside our MySQL Database

**Step 3: Create DAO Class

import java.sql.*;

// Main class public class StudentDAO {

// Class data members
private String driver;
private String url;
private String userName;
private String password;

// Setter methods for dependency injection
public void setDriver(String driver) {
    this.driver = driver;
}
public void setUrl(String url) {
    this.url = url;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public void setPassword(String password) {
    this.password = password;
}

// Method to fetch all student records
public void selectAllRows() throws ClassNotFoundException, SQLException {
    System.out.println("Retrieving all student data..");

    // Load driver
    Class.forName(driver);

    // Establish connection
    Connection con = DriverManager.getConnection(url, userName, password);

    // Execute query
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM studentdb.hostelstudentinfo");

    while (rs.next()) {
        int studentId = rs.getInt(1);
        String studentName = rs.getString(2);
        double hostelFees = rs.getDouble(3);
        String foodType = rs.getString(4);

        System.out.println(studentId + " " + studentName + " " + hostelFees + " " + foodType);
    }

    // Close connection
    con.close();
}

}

`

Step 4: Add Dependencies (JARs)

Step 5: Configure Spring Bean (beans.xml)

**Spring Configuration (beans.xml):

XML `

<bean id="studentDAO" class="StudentDAO">
    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/studentdb"/>
    <property name="userName" value="root"/>
    <property name="password" value="your_password"/>
</bean>

`

Step 6: Create Main Class

// Importing required classes import java.sql.SQLException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

// Main class public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException {

    // Initialize Spring Application Context
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    // Retrieve bean
    StudentDAO studentDAO = context.getBean("studentDAO", StudentDAO.class);

    // Call method to fetch student records
    studentDAO.selectAllRows();
}

}

`

**Output: After running the program, the following output will be displayed:

Retrieving all student data..
1 Asish 300.5 Veg
2 Vicky 245.89 Non Veg
3 Anshul 123.67 Veg

You can see we have successfully fetched the data from the MySQL Database.