zoukankan      html  css  js  c++  java
  • JAVA 数据库切换 -2 (MSSQL 连接)

    public class SqlMsDataSource implements DataSource{
        
        private static SqlMsDataSource instance = new SqlMsDataSource();;
        private SqlMsDataSource() {
            
        }
        public static SqlMsDataSource getInstance() {
           
            return instance;
        }
        
        private static LinkedList<Connection> pool = new LinkedList<Connection>();
        private static  String DriverName = " ";
        private static  String url = " ";
        private static  String username = " ";
        private static  String password = " ";
        private static Logger logger = Logger.getLogger(SqlMsDataSource.class);
        private static Connection conn =null;
        static{
            try {
                ResourceBundle res = ResourceBundle.getBundle("uh");
                url = res.getString("msssql.url"); 
                username = res.getString("msssql.username");  
                password = res.getString("msssql.password"); 
                DriverName = res.getString("msssql.driver"); 
                Class.forName(DriverName);      
                Connection conn = DriverManager.getConnection(url, username, password);
                logger.info("mssql  connect successfully");
            } catch (Exception e) {
                logger.info("mssql connect fail");       
                e.printStackTrace();
                throw new RuntimeException("mssql connect fail");
            }
        }
        
        @Override
        public Connection getConnection() throws SQLException {
            
            if(conn == null || conn.isClosed()) {            
                conn = DriverManager.getConnection(url, username, password);
            }
            return conn;
        }
        
    
        @Override
        public PrintWriter getLogWriter() throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void setLogWriter(PrintWriter out) throws SQLException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void setLoginTimeout(int seconds) throws SQLException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public int getLoginTimeout() throws SQLException {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public <T> T unwrap(Class<T> iface) throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
            // TODO Auto-generated method stub
            return false;
        }
    
    
        @Override
        public Connection getConnection(String username, String password) throws SQLException {
            // TODO Auto-generated method stub
            return null;
        }
    
      
    
    }
  • 相关阅读:
    js-21点小游戏
    js-打印出现最多次的字母
    盒模型浮动
    九九乘法表
    猫眼-湄公河行动电影介绍页面
    (day4)用css画三角形以及红旗
    cookie的使用
    用Servlet校验密码2
    Servlet登录验证
    Servlet概述
  • 原文地址:https://www.cnblogs.com/chuangjie1988/p/14899635.html
Copyright © 2011-2022 走看看