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

    }

  • 相关阅读:
    Jmeter——引入第三方jar包
    Linux系统中安装和卸载node
    Element和Event中的各种offsetTop,clientTop,scrollTop等位置
    关于Linux下LaTeX无法找到已安装字体的问题与解决
    Ubuntu 21.04 使用命令行分配静态IP地址
    批量下载YouTube播放列表(playlist)视频、字幕
    修改tomcat的server.xml,context.xml文件后又自动还原问题
    Echarts legend属性设置
    剑指-30:包含min函数的栈
    无网安装Docker及Docker镜像的导入导出
  • 原文地址:https://www.cnblogs.com/LWQ168/p/7401987.html
Copyright © 2011-2022 走看看