What is Spring – JDBC Framework?

Spring JDBC

Spring Series – Learn JDBC integration by Example

Преглед

In this article we will discuss about the JDBC feature provided by the Spring framework. We will be concentrating more on the implementation and configuration part. This guide will help the developers to follow the steps and implement JDBC using Spring framework.

DAO Basics

DAO stands for data access object which is commonly used for database interaction.DAOs exist to provide a means to read and write data to the database and they should expose this functionality through an interface by which the rest of the application will access them.







Spring JBDC

Spring has its own JDBC framework which helps us to clean up JDBC code by shouldering the burden of resource management and error handling. This leaves you free to concentrate on the statements and queries to get your data to and from the database.

Spring’s data access frameworks incorporate a template class known as JdbcTemplate. A JdbcTemplate only needs Datasource to start working.

Following code snippet shows the creation of JdbcTemplate.

JdbcTemplate template = new JdbcTemplate(myDataSource);

Following code snippet shows the wiring of DAO beans with JdbcTemplate.

<боб ID =”jdbcTemplate”

клас =”org.springframework.jdbc.core.JdbcTemplate”>

<property name=”източник на данни”><ref bean=”източник на данни”/></имот>

</боб>

<боб ID =”studentDao” клас =”StudentDaoJdbc”>

<property name=”jdbcTemplate”><ref bean=”jdbcTemplate”/></имот>

</боб>

<боб ID =”courseDao” клас =”CourseDaoJdbc”>

<property name=”jdbcTemplate”><ref bean=”jdbcTemplate”/></имот>

</боб>







Following is an example of accessing the database using spring JDBC template.

  • The first component is an interface (StudentDAO) to define the method to be implemented. Here the method is to insert the student details.

package com.techalpine.dao;

public interface StudentDAO {

public void insertStudent(Низ, Низ);

}

  • The next component is the implementation class of the above mentioned interface (StudentDAOImpl). It implements the insert method by using JDBC template.

package com.techalpine.dao;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

public class StudentDAOImpl implements StudentDAO {

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {

this.jdbcTemplate = new JdbcTemplate(източник на данни);

}

public void insertStudent(име String, String адрес) {

// Specify SQL query for insert

String query = “INSERT INTO STUDENT (име,address) VALUES (?,?)”;

// Specify the method to be called

jdbcTemplate.update(заявка, new Object[] );

}

}

  • The third component is the configuration file (xml) to define the data source and wiring the beans.

<?xml version=”1.0″ encoding=”UTF-8″?>

<боб xmlns =”HTTP://www.springframework.org/schema/beans”

xmlns:xsi=”HTTP://www.w3.org/2001/XMLSchema-instance”xsi:schemaLocation=”HTTP://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>

<боб ID =”източник на данни” destroy-method=”close” клас =”org.apache.commons.dbcp.BasicDataSource”>

<property name=”driverClassName” value=”org.hsqldb.jdbcDriver”/>

<property name=”url” value=”JDBC:hsqldb:hsql://Localhost”/>

<property name=”username” value=”techalpine”/>

<property name=”password” value=”techalpine”/>

</боб>

<боб ID =”studentDAO” клас =”com.techalpine.dao.StudentDAOImpl”>

<property name=”източник на данни” ref=”източник на данни”/>

</боб>

</боб>

  • Now the final part is a standalone java class (java) to test the sample. The main class retrieve the bean from spring container and then call the insert method to insert the student data.

package com.techalpine.dao;

внос org.springframework.context.ApplicationContext;

внос org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {

публично статично невалидни основни(Низ[] опцията) {

ApplicationContext context = new ClassPathXmlApplicationContext(“student.xml”);

StudentDAO studentDAO = (StudentDAO) context.getBean(“studentDAO”);

studentDAO.insertStudent(“Amit”,”Delhi”);

}

}









Заключение

This article is for the developers who wants to jump into coding without wasting much time on the theoretical part. We have tried to keep the theoretical part minimum and explain more on the coding and configuration. Hope this will help the developers’ community.

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share