eclipse中spring的Spring JdbcTemplate访问access的简易实现
修改上一篇博文中的配置文件和代码,让我们来看看如何访问一个本地access的mdb文件数据库。
首先下图为被访问的access文件的ip地址和存储路径及文件名。
下图为access文件中的某一个表的概要内容
文档结构与前一篇博文完全相同,只是修改了spring配置文件以及sql语句
1.spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd
https://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-4.0.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-4.0.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.infotech.access.dao"/>
<!-- 配置ucanaccess数据源 -->
<bean
destroy-method="close"
p:driverClassName="net.ucanaccess.jdbc.UcanaccessDriver"
p:url="jdbc:ucanaccess://D:\share\data_be.mdb"
p:username="admin"
p:password="" />
<!-- 配置事务管理器 -->
<bean
p:dataSource-ref="dsmysql" />
<!-- 配置JdbcTemplate -->
<bean
p:dataSource-ref="dsmysql" />
</beans>
2.spring源文件
package com.infotech.access.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import com.infotech.access.domain.CheckedList;
@Repository
public class CheckedlistDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* @param skostl
* @return
*/
public String getData(String smname) {
final String sr =null;
jdbcTemplate.query("select * from CheckedList where ID =?", new Object[] { smname }, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
String sr2 = rs.getString("CL_name");
System.out.println(sr2);
}
});
return sr;
}
}
3.测试TestNG文件
package com.infotech.access.dao;
import java.util.Date;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.infotech.access.domain.CheckedList;
import static org.testng.Assert.*;
@ContextConfiguration("classpath*:/ruku-context.xml")
public class CheckedlistDaoTest extends AbstractTransactionalTestNGSpringContextTests{
private CheckedlistDao cld;
@Autowired
public void setCheckedlistDao(CheckedlistDao cldlocal){
this.cld = cldlocal;
}
@Test
public void testAcess() {
String sb1 = this.cld.getData("5");
System.out.println(sb1);
}
}
最后更新:2017-09-28 09:03:07