博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Durid数据库连接池配置(不使用框架)
阅读量:4511 次
发布时间:2019-06-08

本文共 6015 字,大约阅读时间需要 20 分钟。

本人没有使用任何框架,就是纯JAVA的方式

1、下载Durid包(druid-0.2.23.jar)

百度网盘:https://pan.baidu.com/s/1q8LsG_Hv1PosCRwOXOiebw

密码:b3cw

2、web.xml配置

DruidWebStatFilter
com.alibaba.druid.support.http.WebStatFilter
exclusions
*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
DruidWebStatFilter
/*
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
DruidStatView
/druid/*

3、工具类DbPoolConnection(获取connection对象)

1 package com.richinfo.dao.base; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.sql.SQLException; 9 import java.util.Properties;10 11 import com.alibaba.druid.pool.DruidDataSource;12 import com.alibaba.druid.pool.DruidDataSourceFactory;13 import com.alibaba.druid.pool.DruidPooledConnection;14 15 public class DbPoolConnection {16     private static DbPoolConnection databasePool = null;17     private static DruidDataSource dds = null;18     static {19         Properties properties = loadPropertyFile("durid.properties");20         try {21             dds = (DruidDataSource) DruidDataSourceFactory22                     .createDataSource(properties);23         } catch (Exception e) {24             e.printStackTrace();25         }26     }27     private DbPoolConnection() {}28     public static synchronized DbPoolConnection getInstance() {29         if (null == databasePool) {30             databasePool = new DbPoolConnection();31         }32         return databasePool;33     }34     public DruidPooledConnection getConnection() throws SQLException {35         return dds.getConnection();36     }37     public static Properties loadPropertyFile(String fullFile) {38         String webRootPath = null;39         if (null == fullFile || fullFile.equals(""))40             throw new IllegalArgumentException(41                     "Properties file path can not be null : " + fullFile);42         webRootPath = DbPoolConnection.class.getClassLoader().getResource("")43                 .getPath();44         webRootPath = new File(webRootPath).getParent();45         InputStream inputStream = null;46         Properties p = null;47         try {48             inputStream = new FileInputStream(new File(webRootPath49                     + File.separator + fullFile));50             p = new Properties();51             p.load(inputStream);52         } catch (FileNotFoundException e) {53             throw new IllegalArgumentException("Properties file not found: "54                     + fullFile);55         } catch (IOException e) {56             throw new IllegalArgumentException(57                     "Properties file can not be loading: " + fullFile);58         } finally {59             try {60                 if (inputStream != null)61                     inputStream.close();62             } catch (IOException e) {63                 e.printStackTrace();64             }65         }66         return p;67     }68 }

4、工具类(对外提供接口,关闭数据库连接)

1 package com.richinfo.dao.base; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 import org.apache.logging.log4j.LogManager;10 import org.apache.logging.log4j.Logger;11 12 public class DBManager {13     private static Logger log = LogManager.getLogger(DBManager.class);14 15     /**16      * 默认的数据库链接,链接Oss139 统一位置 管理下的对象17      * 18      * @return Connection19      */20     public static Connection getConnection() {21         Connection conn = null;22         try {23              conn = DbPoolConnection.getInstance().getConnection();24         } catch (SQLException e) {25             e.printStackTrace();26             log.error("DBManager getConnection null.{}", e);27             return null;28         }29         return conn;30     }31 32     /**33      * 链接指定的 数据库对象34      * 35      * @param dataBaseName36      * 对象名37      * @return Connection38      */39     public static Connection getConnection(String dataBaseName) {40 41         try {42             Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");43         } catch (ClassNotFoundException e) {44             e.printStackTrace();45             return null;46         } catch (Exception e) {47             e.printStackTrace();48             return null;49         }50         Connection conn = null;51         try {52             conn = DriverManager.getConnection("proxool." + dataBaseName);53         } catch (SQLException e) {54             e.printStackTrace();55             return null;56         }57         return conn;58     }59 60     public static void close(Connection conn, Statement st, ResultSet rs) {61 62         try {63             if (rs != null)64                 rs.close();65         } catch (Exception e) {66             log.error("DBManager close fail rs. {}", e);67         }68         ;69         try {70             if (st != null)71                 st.close();72         } catch (Exception e) {73             log.error("DBManager close fail st. {}", e);74         }75         ;76         try {77             if (conn != null)78                 conn.close();79         } catch (Exception e) {80             log.error("DBManager close fail conn. {}", e);81         }82     }83 }

5、配置文件durid.properties

driverClassName=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@192.168.9.73:1521:ossusername=upmpassword=upmfilters=statinitialSize=2maxActive=300maxWait=60000timeBetweenEvictionRunsMillis=60000minEvictableIdleTimeMillis=300000validationQuery=select CURRENT_DATE FROM dualtestWhileIdle=truetestOnBorrow=falsetestOnReturn=falsepoolPreparedStatements=falsemaxPoolPreparedStatementPerConnectionSize=200

 

转载于:https://www.cnblogs.com/zhuziyu/p/8915584.html

你可能感兴趣的文章
arttemplate模板引擎有假数据返回数据多层内嵌的渲染方法
查看>>
APP为什么会被打回来??
查看>>
word中图片不显示了
查看>>
Chap-3 Section 3.1 目标文件格式
查看>>
用NSSM把.Net Core部署至 Windows 服务
查看>>
CentOS6.5 一键部署运行环境shell脚本
查看>>
springMVC 报错:Unknown return value type: java.lang.Integer
查看>>
jquery easyui 验证Combo和ComboBox
查看>>
Looper,MessageQueue,Message,Handler
查看>>
编程珠玑--变位词集合
查看>>
Centos7 忘记密码的情况下,修改root密码
查看>>
第九章:testng——在Android Eclipse ADT 中 在线安装步骤
查看>>
洛谷 - P1063 - 能量项链 - 区间dp
查看>>
Turn the corner (三分)
查看>>
srs2录制flv文件metadata不准确
查看>>
P2512 [HAOI2008]糖果传递
查看>>
P3224 [HNOI2012]永无乡(平衡树合并)
查看>>
sql2000远程连接失败
查看>>
substring(start,stop)用于提取字符串中介于两个指定下标之间的字符。
查看>>
第三百零六天 how can I 坚持
查看>>