zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
    
    
    }
  • 相关阅读:
    JobTracker作业启动过程分析
    结构体传参
    getchar()
    char *a与char a[n]的区别
    EOF NULL 之间的区别
    现代方法第15章第三节的程序
    交换机console口连接
    undefined reference问题总结
    二维数组与指针
    数组作为参数传递的时候,被调用的函数内无法计算出数组的大小
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/12835860.html
Copyright © 2011-2022 走看看