What is Spring – JDBC Framework?

Spring JDBC

Earraigh Sraith - Foghlaim JDBC Comhtháthú le Abairtí mar shampla,,,en,San Airteagal seo, beimid ag plé mar gheall ar an ghné JDBC dá bhforáiltear le creat Earraigh,,en,Beidh sé seo treoir cabhrú leis an forbróirí a leanúint na céimeanna agus a chur i bhfeidhm JDBC ag baint úsáide as creat Earraigh,,en,Buneolas DAO,,en,Seasann OCC le réad rochtana shonraí a úsáidtear go coitianta le haghaidh interaction.DAOs bunachar ann a a chur bealach a chur ar fáil a léamh agus a scríobh sonraí leis an mbunachar sonraí agus ba chóir dóibh an fheidhm seo a nochtadh trí chomhéadan lena ndéanfar an chuid eile den t-iarratas rochtain a fháil orthu,,en,Spring JBDC,,en,Tá an Earraigh a creat JDBC féin a chabhraíonn linn a ghlanadh suas JDBC cód le shouldering an t-ualach na bainistíochta acmhainní agus láimhseáil earráid,,en,Fágann sé seo leat saor chun díriú ar na ráitis agus ceisteanna a fháil ar do shonraí chuig agus ó bhunachar sonraí,,en

Forbhreathnú

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.

creataí rochtana shonraí Earraigh ionchorprú rang teimpléad a dtugtar JdbcTemplate,,en,A JdbcTemplate gá ach datasource chun tosú ag obair,,en,Tar Blúire cód Léiríonn cruthú JdbcTemplate,,en,JdbcTemplate teimpléad = JdbcTemplate nua,,en,myDataSource,,en,Tar Léiríonn Blúire cód an sreangú de pónairí DAO le JdbcTemplate,,en,JdbcTemplate,,lb,ref Bean =,,en,studentDao,,en,StudentDaoJdbc,,en,courseDao,,en,CourseDaoJdbc,,en,É seo a leanas sampla de rochtain ar an mbunachar sonraí ag baint úsáide as an earraigh JDBC teimpléad,,en,Is é an chéad chomhábhair comhéadan,,en,StudentDAO,,en,a shainmhíniú ar an modh a chur i bhfeidhm,,en,Seo an modh a chur isteach ar an mac léinn na sonraí,,en,com.techalpine.dao pacáiste,,en,StudentDAO chomhéadan poiblí,,en,insertStudent neamhní poiblí,,en,Is é an chomhpháirt eile an rang chun feidhme an comhéadan thuasluaite,,en,StudentDAOImpl,,en,Cuireann sé i bhfeidhm an modh isteach trí úsáid a bhaint teimpléad JDBC,,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”/></maoin>

</bean>

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

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

</bean>

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

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

</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(Teaghrán, Teaghrán);

}

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

allmhairiú java.sql.ResultSet,,en,allmhairiú java.sql.SQLException,,en,allmhairiú javax.sql.DataSource,,en,allmhairiú org.springframework.jdbc.core.JdbcTemplate,,en,allmhairiú org.springframework.jdbc.core.RowMapper,,en,Cuireann rang poiblí StudentDAOImpl StudentDAO,,en,príobháideach JdbcTemplate jdbcTemplate,,en,setDataSource neamhní poiblí,,en,datasource datasource,,en,this.jdbcTemplate = JdbcTemplate nua,,en,Sonraigh cheist SQL le haghaidh chur isteach,,en,cheist Teaghrán =,,en,ISTEACH AN INTO MAC LÉINN,,en,LUACHANNA,,en,Sonraigh an modh a bheidh le glaoch,,en,Réad nua,,en,Is é an tríú comhábhar an comhad cumraíochta,,en,a shainmhíniú ar an fhoinse sonraí agus sreangú na pónairí,,en,www.springframework.org/schema/beans http,,en,www.springframework.org/schema/beans/spring-beans.xsd,,en,scrios-method =,,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,Anois tá an chuid deiridh rang java standalone,,en,a thástáil ar an sampla,,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 (ainm,address) VALUES (?,?)”;

// Specify the method to be called

jdbcTemplate.update(cheist, 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. An rang is mó a aisghabháil an Bean ó choimeádán earrach agus ansin glaoch ar an modh isteach a chur isteach ar na sonraí mac léinn,,en,Mac Léinn rang poiblí,,en,ApplicationContext comhthéacs = ClassPathXmlApplicationContext nua,,en,student.xml,,en,StudentDAO studentDAO =,,en,studentDAO.insertStudent,,en,cad,,hu,Deilí,,en,Tá an tAirteagal seo do na forbróirí atá ag iarraidh chun léim isteach códú gan wasting cuid mhór ama ar an chuid teoiriciúil,,en,Tá iarracht déanta againn a choinneáil ar an chuid is lú teoiriciúil agus míniú a thabhairt níos mó ar an códú agus cumraíocht,,en,Tá súil agam go mbeidh sé seo cabhrú pobal na forbróirí ',,en.

package com.techalpine.dao;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {

statach neamhní príomh phoiblí(Teaghrán[] comhbhrí leis an rogha) {

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

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

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

}

}









Conclúid

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