eclipse中spring的Spring JdbcTemplate访问access的简易实现

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 修改上一篇博文中的配置文件和代码,让我们来看看如何访问一个本地access的mdb文件数据库。 首先下图为被访问的access文件的ip地址和存储路径及文件名。 下图为access文件中的某一个表的概要内容 下图表示eclipse所在的就是同一台电脑 文档结构与前一篇博文完全相同,只是修改了spring配置文件以及sql语句 1.

修改上一篇博文中的配置文件和代码,让我们来看看如何访问一个本地access的mdb文件数据库。
首先下图为被访问的access文件的ip地址和存储路径及文件名。
_
下图为access文件中的某一个表的概要内容
_

下图表示eclipse所在的就是同一台电脑
_

文档结构与前一篇博文完全相同,只是修改了spring配置文件以及sql语句
1.spring配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd     
        http://www.springframework.org/schema/context
        classpath:/org/springframework/context/config/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">     
        
    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.infotech.access.dao"/>
        
    <!-- 配置ucanaccess数据源 -->
    <bean id="dsmysql" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="net.ucanaccess.jdbc.UcanaccessDriver"
        p:url="jdbc:ucanaccess://D:\share\data_be.mdb"
        p:username="admin"
        p:password="" />
        
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dsmysql" />

    <!-- 配置JdbcTemplate  -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        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);
        
    }
    

}

_
_

下面我们将access复制到同一局域网的另一台电脑上,进行远程访问
_

只需要修改spring配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd     
        http://www.springframework.org/schema/context
        classpath:/org/springframework/context/config/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">     
        
    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.infotech.access.dao"/>
        
    <!-- 配置ucanaccess数据源 -->
    <bean id="dsmysql" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="net.ucanaccess.jdbc.UcanaccessDriver"
        p:url="jdbc:ucanaccess://\\\\172.16.30.106\\share\\data_be.mdb"
        p:username="admin"
        p:password="" />
        
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dsmysql" />

    <!-- 配置JdbcTemplate  -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        p:dataSource-ref="dsmysql" />
     
</beans>  

_

下面基于上述内容扩充我们代码的内容,java代码和测试TestNG代码如下所示:

package com.infotech.access.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.infotech.access.domain.CheckedList;

@Repository
public class CheckedlistDao {

    /**
     * 声明JdbcTemplate的一个变量
     */
    private JdbcTemplate jdbcTemplate;

    /**
     * @param 注入JdbcTemplate的变量的实例
     */
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    

    /**
     * @param sCname 输入参数为id
     * @return 返回CheckedList对象
     */
    public CheckedList getCL(final String sid) {

        final CheckedList cl1 = new CheckedList();

        jdbcTemplate.query("select * from CheckedList where ID =?", new Object[] { sid }, new RowCallbackHandler() {

            public void processRow(ResultSet rs) throws SQLException {
                cl1.setSid(sid);
                cl1.setClName(rs.getString("CL_name"));
                cl1.setClHandler(rs.getString("CL_handler"));
            }

        });
        return cl1;
    }
    
    /**
     * @param fromId
     * @param toId
     * @return 返回值是List容器的实例
     */
    public List<CheckedList> getCLs(final String fromId, final String toId) {

        String sql2 = "select * from CheckedList where ID between ? and ?";
        final List<CheckedList> cls = new ArrayList<CheckedList>();
        jdbcTemplate.query(sql2, new Object[] { fromId, toId }, new RowCallbackHandler() {

            public void processRow(ResultSet rs) throws SQLException {
                CheckedList cl1 = new CheckedList();
                cl1.setSid(rs.getString("ID"));
                cl1.setClName(rs.getString("CL_name"));
                cl1.setClHandler(rs.getString("CL_handler"));
                cls.add(cl1);
            }
        });

        return cls;
    }
    
    /**
     * 重构 
     * @param sHname 输入参数为CL_handler 模糊查询
     * @return
     */
    public List<CheckedList> getCLs(String sHname) {

        String sql = "select * from CheckedList where CL_handler like ?";
        return jdbcTemplate.query(sql, new Object[] { sHname }, new RowMapper<CheckedList>() {

            public CheckedList mapRow(ResultSet rs, int rowNum) throws SQLException {

                CheckedList cl1 = new CheckedList();
                cl1.setSid(rs.getString("ID"));
                cl1.setClName(rs.getString("CL_name"));
                cl1.setClHandler(rs.getString("CL_handler"));
                return cl1;
            }
        });

    }
    
    /**
     * @param sid  返回值是数据库中一条记录中的一个字段值
     * @return
     */
    public String getCLone(String sid){
        
        String sql = "select * from CheckedList where ID =?";
        String sr = jdbcTemplate.queryForObject(sql, new Object[] {sid}, new RowMapper<String>(){

            public String mapRow(ResultSet rs, int rowNum) throws SQLException {
                // TODO Auto-generated method stub
                return rs.getString("CL_name");
            }
            
        });
        
        return sr;
    }
    
    
    
    /**
     * @param 
     * @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;
    }
    

}

测试代码

package com.infotech.access.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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);                    */
        
/*        CheckedList cltemp = cld.getCL("5");
        String sHname = cltemp.getClHandler();
        String sName = cltemp.getClName();
        assertEquals(cltemp.getClHandler(),"许强");
        System.out.println(sHname+"--"+sName);      */
        
/*        List<CheckedList> rlist = cld.getCLs("5", "10");
        
        boolean b1 = rlist.isEmpty();   //是否为空
        int i1 = rlist.size();          //结果集大小        
        System.out.println(b1);
        System.out.println(i1);
        
        for (int i = 0; i < i1; i++) {   //循环显示ArrayList的内容
            CheckedList cltemp = rlist.get(i);
            String sName = cltemp.getClName();
            String sHname = cltemp.getClHandler();
            System.out.println(sHname+"  "+sName);
        }*/
        
/*        List<CheckedList> rlist = cld.getCLs("许强");
        boolean b1 = rlist.isEmpty();   //是否为空
        int i1 = rlist.size();          //结果集大小        
        System.out.println(b1);
        System.out.println(i1);
        
        for (int i = 0; i < i1; i++) {   //循环显示ArrayList的内容
            CheckedList cltemp = rlist.get(i);
            String sName = cltemp.getClName();
            String sHname = cltemp.getClHandler();
            System.out.println(sHname+"  "+sName);
        }*/
        
        String s2 = cld.getCLone("5");   //获取一个String
        System.out.println(s2);
        
        
    }
    

}

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
Java Spring
使用JDBCTemplate实现与Spring结合,方法公用 ——测试(EmpDaoImplTest)
使用JDBCTemplate实现与Spring结合,方法公用 ——测试(EmpDaoImplTest)
9 0
|
5月前
|
XML Java 数据库连接
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
50 0
|
3月前
|
安全 Java 数据库
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
|
1天前
|
Java Spring
spring boot访问接口报500
spring boot访问接口报500
7 2
|
4月前
|
XML Java 关系型数据库
Spring6 JdbcTemplate和事务
Spring6 JdbcTemplate和事务
|
26天前
|
设计模式 Java 数据库连接
【Spring源码】JDBC数据源访问实现
我们再来看看阅读线索三,这方面我们从设计模式进行入手。阅读线索三:从这个模块可以学到什么我们看下以下代码,PreparedStatement实例的是由PreparedStatementCreator实现的。再来看看PreparedStatementCreator接口,一共有三个子类实现。也就是说PreparedStatement的三种不同实现被封装到三个子类中,而具体需要哪种实现,只需要传入不同。
24 0
【Spring源码】JDBC数据源访问实现
|
29天前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
2月前
|
Java Spring
使用JDBCTemplate实现与Spring结合,方法公用 ——Emp实现类(EmpDaoImpl)
使用JDBCTemplate实现与Spring结合,方法公用 ——Emp实现类(EmpDaoImpl)
8 0
|
2月前
|
Java Spring
使用JDBCTemplate实现与Spring结合,方法公用 ——共用实现类(BaseImpl)
使用JDBCTemplate实现与Spring结合,方法公用 ——共用实现类(BaseImpl)
10 1
|
2月前
|
Java Spring
使用JDBCTemplate实现与Spring结合,方法公用 ——接口(BaseDao)
使用JDBCTemplate实现与Spring结合,方法公用 ——接口(BaseDao)
9 0

推荐镜像

更多