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();
            }    
        }
        

    }

  • 相关阅读:
    Super超级ERP系统---(1)总体设计
    推荐三款强大的Js图表库
    PHP session锁
    关于MVC的一些思考
    git 设置ssh无密码登录
    一个临时性页面的优化
    Redis系列三:Redis常用设置
    根据省份等地址获取经纬度,或根据经纬度获取地址信息
    Redis系列二:Redis支持的数据类型和使用方法(二)
    Redis系列二:Redis支持的数据类型和使用方法(一)
  • 原文地址:https://www.cnblogs.com/LWQ168/p/7401987.html
Copyright © 2011-2022 走看看