What is Spring – JDBC Framework?

Spring JDBC

Spring Series - Naučte JDBC integraci příkladem,,en,V tomto článku se budeme diskutovat o funkci JDBC poskytnutých rámci jarního,,en,Tato příručka vám pomůže vývojářům následovat kroky a zavést JDBC s použitím rámec Spring,,en,DAO Základy,,en,DAO je zkratka pro přístup k datům objektu, který se běžně používá pro databázové interaction.DAOs existují poskytnout prostředky pro čtení a zápis dat do databáze a měly by vystavit tuto funkci prostřednictvím rozhraní, kterou zbytek aplikace je bude přistupovat,,en,jarní JBDC,,en,Pružina má svůj vlastní rámec JDBC, která nám pomáhá vyčistit JDBC kód svých bedrech břemeno řízení zdrojů a zpracování chyb,,en,To vás nechá soustředit na prohlášení a dotazy, aby si svá data do az databáze,,en

Přehled

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.

přístup k datům rámce jaře je začlenit šablonu třídy známý jako JdbcTemplate,,en,JdbcTemplate potřebuje pouze DataSource začít pracovat,,en,Následující fragment kódu ukazuje vytvoření JdbcTemplate,,en,JdbcTemplate template = nový JdbcTemplate,,en,myDataSource,,en,Následující fragment kódu ukazuje zapojení DAO fazole s JdbcTemplate,,en,JdbcTemplate,,lb,ref = fazole,,en,studentDao,,en,StudentDaoJdbc,,en,courseDao,,en,CourseDaoJdbc,,en,Následující text je příkladem přístupu k databázi pomocí pružiny JDBC šablony,,en,První složkou je rozhraní,,en,StudentDAO,,en,definovat způsob mají být provedeny,,en,Zde je metoda vložit podrobnosti studentské,,en,balíček com.techalpine.dao,,en,veřejné rozhraní StudentDAO,,en,public void insertStudent,,en,Dalším komponentem je implementace třídy výše uvedené rozhraní,,en,StudentDAOImpl,,en,To realizuje způsob vložení pomocí JDBC šablony,,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=”zdroj dat”><ref bean=”zdroj dat”/></majetek>

</fazole>

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

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

</fazole>

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

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

</fazole>







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(Řetěz, Řetěz);

}

  • 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,,en,import java.sql.SQLException,,en,import javax.sql.DataSource,,en,import org.springframework.jdbc.core.JdbcTemplate,,en,import org.springframework.jdbc.core.RowMapper,,en,public class StudentDAOImpl implementuje StudentDAO,,en,soukromého JdbcTemplate jdbcTemplate,,en,public void setDataSource,,en,DataSource Datasource,,en,this.jdbcTemplate = new JdbcTemplate,,en,Specifikovat SQL dotaz pro vložku,,en,String query =,,en,INSERT INTO STUDENT,,en,HODNOTY,,en,Zvolit metodu, která se nazývá,,en,new Object,,en,Třetí složkou je konfigurační soubor,,en,definovat zdroj dat a zapojení fazole,,en,www.springframework.org/schema/beans http,,en,www.springframework.org/schema/beans/spring-beans.xsd,,en,zničit metoda =,,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,ref =,,en,Nyní je závěrečná část je samostatný java class,,en,testovat vzorek,,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(zdroj dat);

}

public void insertStudent(String name, String address) {

// Specify SQL query for insert

String query = “INSERT INTO STUDENT (jméno,address) VALUES (?,?)”;

// Specify the method to be called

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

<fazole 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 =”zdroj dat” 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”/>

</fazole>

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

<property name=”zdroj dat” ref=”zdroj dat”/>

</fazole>

</fazole>

  • Now the final part is a standalone java class (java) to test the sample. Hlavní třída získat fazole z pružinové kontejneru a pak volání metody insert pro vložení dat studentské,,en,public class Student,,en,ApplicationContext kontext = new ClassPathXmlApplicationContext,,en,student.xml,,en,StudentDAO studentDAO =,,en,studentDAO.insertStudent,,en,co,,hu,Delhi,,en,Tento článek je určen pro vývojáře, kteří chtějí skočit do kódování bez plýtvání hodně času na teoretické části,,en,Snažili jsme se zachovat minimální teoretickou část a vysvětlit více o kódování a konfigurace,,en,Doufám, že to pomůže společenství pro vývojáře,,en.

package com.techalpine.dao;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {

public static void main(Řetěz[] args) {

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

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

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

}

}









Závěr

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