package com.saye.hospitalgd.util;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author thuang
 * @version 1.0
 * @description:
 * 通过连接获取Oracle中的数据。
 * @date 2021/9/16 15:07
 */
public class connOracleDate {

    public HashMap<Object,Object> getData(String url,String driverClassName,String name,String password,String sql){
        HashMap<Object,Object> responseMap=new HashMap<>();
        String errCode="0";
        String errMsg="";

        try {
            JdbcTemplate jOracle = connectSql(url, driverClassName, name, password);
            List<Map<String, Object>> list = jOracle.queryForList(sql);
            responseMap.put("dataList",list);
        }catch (Exception e){
            e.printStackTrace();
            errCode="999";
            errMsg="连接Oracle获取数据错误，原因："+e.getMessage();
        }
        responseMap.put("errCode",errCode);
        responseMap.put("errMsg",errMsg);


        return responseMap;
    }

    public JdbcTemplate connectSql(String url,String driverClassName,String name,String password){
        DriverManagerDataSource dataSource =new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(name);
        dataSource.setPassword(password);
        return new JdbcTemplate(dataSource);
    }

}
