zoukankan      html  css  js  c++  java
  • java dbcp连接oracle数据库

    public class DBUtil {
        
        private static BasicDataSource bs;
        
        static{
            //读取配置文件db.properties
            Properties pt=new Properties();
            try {
                pt.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
                String driver= pt.getProperty("driver");
                String url=pt.getProperty("url");
                String user=pt.getProperty("user");
                String pwd=pt.getProperty("pwd");
                String initsize= pt.getProperty("initsize");
                String maxsize=pt.getProperty("maxsize");
                bs=new BasicDataSource();
                bs.setDriverClassName(driver);
                bs.setUrl(url);
                bs.setUsername(user);
                bs.setPassword(pwd);
                bs.setInitialSize(new Integer(initsize));
                bs.setMaxActive(new Integer(maxsize));
                
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("加载配置文件失败");     
            }
        }
        
        //连接数据库方法
        public static Connection getConnection() throws SQLException{
                return bs.getConnection();
        }
        
        //关闭数据库连接
        public static void close(Connection conn){
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("关闭连接失败");
                }
            }
            
        }
        
        //测试    是否连接oracle数据库
        public static void main(String[] args) {
            try {
                Connection conn=DBUtil.getConnection();
                System.out.println(conn);
                
            } catch (SQLException e) {
                e.printStackTrace();
            }    
        }
        

    }

  • 相关阅读:
    如何学好编程
    进制转换
    第五周学习总结 20201204 于瀛鹏
    xor运算
    20201204 于瀛鹏 第四周学习总结
    20201204 于瀛鹏 第三周学习总结
    IEEE754浮点数
    base64编码
    罗马数字(1-3999)转阿拉伯数字
    俄罗斯方块
  • 原文地址:https://www.cnblogs.com/LWQ168/p/7401987.html
Copyright © 2011-2022 走看看