Using spring's jdbctemplate to access the database, go

Source: Internet
Author: User

Accessing the database using Spring's JdbcTemplate

The JdbcTemplate template simplifies the JDBC operation, but creating a jdbctemplate requires a DataSource interface, which in spring is, of course, injecting a datasource into the JdbcTemplate, Then get a connection (Connection) through JdbcTemplate.

Suppose you have a person table in the SQL Server 2000 database (the new database name is Hibernate), which simply records the details of the people.

Using spring's IOC mechanism to implement the injection, the configuration XML code looks like this:

<?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-2.0.xsd ">


<bean id= "Springdatasource"
   class= " Org.springframework.jdbc.datasource.DriverManagerDataSource "
   <property name=" Driverclassname "
    value=" Com.microsoft.jdbc.sqlserver.SQLServerDriver ";
   </property>
   <property name= "url"
    value= "jdbc:microsoft:sqlserver:/ /localhost:1433;databasename=hibernate "
   </property>
   <property name=" Username "value=" sa "></PROPERTY>
   <property name=" password "value=" 111111 "></ Property>
</bean>


<bean id= "JdbcTemplate"
class= "Org.springframework.jdbc.core.JdbcTemplate" abstract= "false"
Lazy-init= "false" autowire= "default" dependency-check= "Default" >
<property name= "DataSource" >
<ref bean= "Springdatasource"/>
</property>
</bean>

</beans>

First write a Persondao, and the code looks like this:

Package Org.shirdrn.jdbcTemplate.dao;

Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import Javax.sql.DataSource;

Import Org.shirdrn.interf.IPersonDao;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.jdbc.core.JdbcTemplate;

public class Persondao implements Ipersondao {
Private JdbcTemplate JdbcTemplate;

public void Setjdbctemplate (JdbcTemplate jdbctemplate) {//injection JdbcTemplate
This.jdbctemplate = JdbcTemplate;
}

Public Connection getconnection () {//Get Connection
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
DataSource Springdatasource = (DataSource) ctx.getbean ("Springdatasource");
JdbcTemplate JdbcTemplate = (jdbctemplate) ctx.getbean ("JdbcTemplate");
/*jdbctemplate.setdatasource (Springdatasource);
Setjdbctemplate (JdbcTemplate); */
DataSource DataSource = Jdbctemplate.getdatasource ();
Connection conn = null;
try {
conn = Datasource.getconnection ();
} catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}

Public ResultSet querypersons (String sql) {
ResultSet rs = null;
try {
Connection Connection = getconnection ();
Statement st = Connection.createstatement ();
rs = st.executequery (SQL);
} catch (SQLException e) {
E.printstacktrace ();
}
Return RS;
}
}

The two lines of code commented out above:

Jdbctemplate.setdatasource (Springdatasource);
Setjdbctemplate (JdbcTemplate);

In fact, the spring IOC container is automatically detected and automatically injected.

To establish a test main function:

Package org.shirdrn.main;

Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import Org.shirdrn.jdbcTemplate.dao;

public class Main {

public static void Main (string[] args) {
Persondao PD = new Persondao ();
ResultSet rs = pd.querypersons ("SELECT * from person");
int n = 0;
try {
while (Rs.next ()) {
n++;
SYSTEM.OUT.PRINTLN ("id =" +rs.getstring ("id"));
System.out.println ("name =" +rs.getstring ("name"));
SYSTEM.OUT.PRINTLN ("gender =" +rs.getstring ("gender"));
System.out.println ("Age =" +rs.getstring ("Age"));
System.out.println ("addr =" +rs.getstring ("addr"));
System.out.println ("*******************************");
}
SYSTEM.OUT.PRINTLN ("The database has a record of" +n+ "article");
} catch (SQLException e) {
E.printstacktrace ();
}
}
}

Ensure that there is test data in the database table, and (my) run the program result output as follows:

id = 222403199901011111
Name = Liu Bei
Gender = Male
Age = 21
addr = Beijing
*******************************
id = 222403199901011112
Name = Zhang Fei
Gender = Male
Age = 23
addr = Tianjin
*******************************
id = 222403199901011113
name = LV Bu
Gender = Male
Age = 22
addr = Shanghai
*******************************
id = 222403199901011114
name = Mink Cicada
gender = female
Age = 22
addr = Dalian
*******************************
id = 222403199901011115
Name = Dong Zhuo
Gender = Male
Age = 20
addr = Changchun
*******************************
id = 222403199901011116
name = Guan Yu
Gender = Male
Age = 18
addr = Beijing
*******************************
id = 222403199901011117
Name = Li Kui
gender = female
Age = 19
addr = Chengdu
*******************************
id = 222403199901011118
name = Song Jiang
gender = female
Age = 21
Addr = Qingdao
*******************************
id = 222403199901011119
Name = Lin
Gender = Male
Age = 23
addr = Hangzhou
*******************************
id = 222403199901011120
Name = High Qiu
Gender = Male
Age = 21
addr = Dunhua
*******************************
id = 222403199901011121
Name = Chungai
Gender = Male
Age = 20
addr = Guangzhou
*******************************
id = 222403199901011122
Name = Harry
gender = female
Age = 18
addr = Shenzhen
*******************************
id = 222403199901011123
Name = Zhang San
Gender = Male
Age = 17
addr = Lanzhou
*******************************
id = 222403199901011124
Name = John Doe
gender = female
Age = 25
addr = Beijing
*******************************
id = 222403199901011125
Name = Zhaoqi
Gender = Male
Age = 28
addr = Hong Kong
*******************************
There are 15 records in the database.

Note The DataSource is an instance of Org.springframework.jdbc.datasource.DriverManagerDataSource, then injects the datasource that has been acquired into a jdbctemplate, and then builds the The connection to the database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.