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

    }

  • 相关阅读:
    理解BSTR数据类型 神奇的BSTR
    char *
    _variant_t和_bstr_t
    数据库中创建表(包括创建主键,外键,非空列,唯一)
    使用ADO实现BLOB数据的存取 -- ADO开发实践之二
    sql server 2005 修改动态端口,连接字符串为:需要改成:IP地址+逗号+端口号才行
    Bilateral Filtering(双边滤波) for SSAO
    关于在Arduino中调用DS1302模块
    关于电机驱动扩展板 L293D 马达板Arduino
    Arduino教程资料汇总(8月22日悄悄跟新了一下)
  • 原文地址:https://www.cnblogs.com/LWQ168/p/7401987.html
Copyright © 2011-2022 走看看