• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
SqlServerDruidUtil

配置了yml文件

package com.tylin.erpreport.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.core.io.ClassPathResource;
import java.io.InputStream;
import java.sql.*;


public class SqlServerDruidUtil {

    private static  String DBDRIVER = null;// 驱动类类名

    private static  String DBURL = null;

    private static  String DBUSER = null;

    private static  String DBPASSWORD = null;


    static{
        try {
            ClassPathResource resource = new ClassPathResource("application.yml");
            InputStream inputStream = resource.getInputStream();
            YamlUtils.setYmlInputSteam(inputStream);
            String profileActive = YamlUtils.getByKey("spring.profiles.active").toString();

            resource = new ClassPathResource("application-"+profileActive+".yml");
            inputStream = resource.getInputStream();
            YamlUtils.setYmlInputSteam(inputStream);
            DBDRIVER = YamlUtils.getByKey("spring.datasource.driverClassName").toString();
            DBURL = YamlUtils.getByKey("spring.datasource.url").toString();
            DBUSER = YamlUtils.getByKey("spring.datasource.username").toString();
            DBPASSWORD = YamlUtils.getByKey("spring.datasource.password").toString();
        }catch (Exception e){
            throw new RuntimeException("SqlServerUtil static Exception "+e.getMessage());
        }

    }


    public SqlServerDruidUtil(){
    }

    public static Connection getConnection()throws Exception {
        Connection conn = null;

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(DBDRIVER);
        dataSource.setUrl(DBURL);
        dataSource.setUsername(DBUSER);
        dataSource.setPassword(DBPASSWORD);

        conn = dataSource.getConnection();

        return conn;
    }


    public static ResultSet executeQuery(String sql) throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {


            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            // 执行sql:
            rs = pstmt.executeQuery();
            return rs;

        } catch (SQLException sqle) {

            throw new SQLException("select data Exception: "
                    + sqle.getMessage());

        } catch (Exception e) {

            throw new Exception("System error: " + e.getMessage());

        }finally {
            closeConnection(null, pstmt, conn);
        }

    }


    public static void executeUpdate(String sql) throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        try {
            conn =getConnection();
            ps =  conn.prepareStatement(sql);
            ps.executeUpdate();

        } catch (SQLException sqle) {

            throw new SQLException("select data Exception: "
                    + sqle.getMessage());

        } catch (Exception e) {

            throw new Exception("System error: " + e.getMessage());

        }finally {
            closeConnection(null, ps, conn);
        }

    }

    public static void closeConnection(ResultSet rs, PreparedStatement ps, Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }

            if(ps!=null){
                ps.close();
            }

            if(conn!=null){
                conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

    }


}

 

posted on 2020-05-06 13:37  红无酒伤  阅读(393)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3