What is Spring – JDBC Framework?

Spring JDBC

Pavasario serija - Sužinokite JDBC integraciją pavyzdį,,en,Šiame straipsnyje mes aptarsime apie JDBC funkcija, kurią teikia pavasario sistemos,,en,Šis vadovas padės kūrėjams atlikite ir įgyvendinti JDBC naudojant Spring Framework,,en,DAO pagrindai,,en,Dao reiškia duomenų prieigos objekto, kuris yra visuotinai naudojamas duomenų bazių interaction.DAOs egzistuoja numatyti priemones, skaityti ir rašyti duomenis į duomenų bazę, ir jie turėtų atskleisti šį funkcionalumą per sąsają, kuria paraiškos poilsio bus pasiekti juos,,en,Pavasario JBDC,,en,Pavasaris turi savo JDBC sistemą, kuri padeda mums išvalyti JDBC kodą prisiimti tausaus išteklių valdymo ir apdorojimo klaidų naštą,,en,Tai palieka jums nemokamai sutelkti dėmesį į atskaitomybės ir užklausas gauti savo duomenis į ir iš duomenų bazės,,en

Apžvalga

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.

Pavasarį duomenų prieigos sistemas įtraukti šabloną klasę, vadinamą JdbcTemplate,,en,JdbcTemplate reikia tik duomenųšaltinio pradėti dirbti,,en,Po kodo fragmentą rodo JdbcTemplate kūrimą,,en,JdbcTemplate šablonas = nauji JdbcTemplate,,en,myDataSource,,en,Po kodo fragmentą rodo DAO pupelės su JdbcTemplate laidų,,en,JdbcTemplate,,lb,studentDao,,en,StudentDaoJdbc,,en,courseDao,,en,CourseDaoJdbc,,en,Toliau pateikiamas patekti į duomenų bazę, naudojant pavasario JDBC šablono pavyzdys,,en,Pirmasis komponentas yra sąsaja,,,en,StudentDAO,,en,apibrėžti metodus, kurie bus įgyvendinami,,en,Čia metodas yra įterpti studentas detales,,en,paketas com.techalpine.dao,,en,visuomenės sąsaja StudentDAO,,en,public void insertStudent,,en,Kitas komponentas yra įgyvendinti klasė Minėti sąsaja,,en,StudentDAOImpl,,en,Ji įgyvendina įterpti metodą naudojant JDBC šabloną,,en. 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.

<bean id=”jdbcTemplate”

class=”org.springframework.jdbc.core.JdbcTemplate”>

<property name=”dataSource”><ref bean=”dataSource”/></turtas>

</bean>

<bean id=”studentDao” class=”StudentDaoJdbc”>

<property name=”jdbcTemplate”><ref bean=”jdbcTemplate”/></turtas>

</bean>

<bean id=”courseDao” class=”CourseDaoJdbc”>

<property name=”jdbcTemplate”><ref bean=”jdbcTemplate”/></turtas>

</bean>







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(Styga, Styga);

}

  • 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;

importo java.sql.ResultSet,,en,importo java.sql.SQLException,,en,importo javax.sql.DataSource,,en,importo org.springframework.jdbc.core.JdbcTemplate,,en,importo org.springframework.jdbc.core.RowMapper,,en,visuomenės klasė StudentDAOImpl įgyvendina StudentDAO,,en,asmeninįpranešimą JdbcTemplate jdbcTemplate,,en,public void setDataSource,,en,DataSource duomenųšaltinio,,en,this.jdbcTemplate = nauja JdbcTemplate,,en,Nurodykite SQL užklausą, įdėklu,,en,Eilutė užklausos =,,en,INSERT INTO STUDENTŲ,,en,VERTYBĖS,,en,Nurodykite metodą galima pavadinti,,en,naujas objektas,,en,Trečiasis komponentas yra konfigūracijos failą,,en,apibrėžti duomenų šaltinį ir laidų pupelės,,en,www.springframework.org/schema/beans http,,en,www.springframework.org/schema/beans/spring-beans.xsd,,en,sunaikinti-metodas =,,en,org.apache.commons.dbcp.BasicDataSource,,en,driverClassName,,en,org.hsqldb.jdbcDriver,,en,hsqldb,,en,hsql,,en,techalpine,,en,studentDAO,,en,com.techalpine.dao.StudentDAOImpl,,pt,Dabar baigiamoji dalis yra atskira Java klasės,,en,išbandyti mėginį,,en;

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(dataSource);

}

public void insertStudent(String name, String address) {

// Specify SQL query for insert

String query = “INSERT INTO STUDENT (pavadinimas,address) VALUES (?,?)”;

// Specify the method to be called

jdbcTemplate.update(query, 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″?>

<beans 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”>

<bean id=”dataSource” destroy-method=”close” class=”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”/>

</bean>

<bean id=”studentDAO” class=”com.techalpine.dao.StudentDAOImpl”>

<property name=”dataSource” ref=”dataSource”/>

</bean>

</beans>

  • 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;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {

public static negaliojančiu pagrindinis(Styga[] args) {

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

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

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

}

}









Išvada

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