閱讀684 返回首頁    go 技術社區[雲棲]


eclipse中spring的Spring JdbcTemplate訪問access的簡易實現

修改上一篇博文中的配置文件和代碼,讓我們來看看如何訪問一個本地access的mdb文件數據庫。
首先下圖為被訪問的access文件的ip地址和存儲路徑及文件名。
_
下圖為access文件中的某一個表的概要內容
_

下圖表示eclipse所在的就是同一台電腦
_

文檔結構與前一篇博文完全相同,隻是修改了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

  上一篇:go  用shell腳本合並多個文件內容
  下一篇:go  Docker 鏡像優化與最佳實踐