基于 Apache DBCP 的数据库连接获取类(原创)
Posted on基于 Apache DBCP 的数据库连接获取类(原创) - BeanSoft's Java Blog - BlogJava
MyEclipse 教程, Java EE 5, JSPWiki, Spring, Struts, Hibernate, JPA, SWT, Swing, AJAX, JavaScript, Netbeans
导航
随笔分类
- AJAX(22) (rss)
- Database(11) (rss)
- Eclipse(30) (rss)
- Firefox(3) (rss)
- Free Software(20) (rss)
- Game(4) (rss)
- Hibernate(8) (rss)
- Java Code Share(12) (rss)
- Java EE(32) (rss)
- Java SE(27) (rss)
- JavaMail(5) (rss)
- JavaScript(1) (rss)
- JBoss(3) (rss)
- JSF(4) (rss)
- JSP(3) (rss)
- JSPWiki(16) (rss)
- Michael Jackson(1) (rss)
- My Open Source Toys(30) (rss)
- MyEclipse(43) (rss)
- NetBeans(15) (rss)
- Open Source World(14) (rss)
- Oracle(2) (rss)
- Other(72) (rss)
- Portable Java IDE(3) (rss)
- ROR(2) (rss)
- Spring(29) (rss)
- Swing, GUI(9) (rss)
- SWT/JFACE/RCP(24) (rss)
- Tomcat(1) (rss)
- Web(42) (rss)
- Web Framework(8) (rss)
- Weblogic(7) (rss)
- What's Hot(17) (rss)
- 战地2 Battle Field 2(2) (rss)
- 老军医悬壶济世(13) (rss)
最新随笔
- 1. 关于MyEclipse 6.0.0 GA开发SSH应用的%%%% Error Creating SessionFactory %%%% java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"'异常的解决方案
- 2. 可以在多普达微软 Pocket PC Windows Mobile 5 手机上运行的eswt程序包
- 3. BlogJava 备份文章阅读器:增加日期范围增量备份功能
- 4. 电子书 《MyEclipse 6 Java 开发中文教程》 第十章 开发 Spring 应用 完工
- 5. 如何购买《MyEclipse 6 Java 开发中文教程》DVD光盘(含源代码,视频和软件)及网上答疑
- 6. IBM 中文网站的 Spring 系列等资料
- 7. Spring 1.2和2.0的简单AOP例子
- 8. Word 文档加入带链接功能的目录的方法
- 9. 电子书 《MyEclipse 6 Java 开发中文教程》 第九章 开发Struts 1.x应用 完工
- 10. 电子书 《MyEclipse 6 Java 开发中文教程》第八章 开发 Web 应用
- 11. 电子书 《MyEclipse 6 Java 开发中文教程》 第七章 Hibernate 开发预览
- 12. MyEclipse 6 实战开发讲解视频入门 11 Struts 文件上传
- 13. 电子书 《MyEclipse 6 Java 开发中文教程》 第五,六章预览
- 14. 原创电子书 《MyEclipse 6 Java 开发中文教程》下载
- 15. 感谢 2007 年 12 月的 <<程序员>> 杂志推荐我的 Blog
- 16. 视频讲解: Eclipse/MyEclipse 如何导入项目源代码
- 17. MyEclipse 6 实战开发讲解视频入门 10 JSP 文件上传下载
- 18. Java SE 6 Update N Early Access 提供下载
- 19. Spring 2.5 标注开发的简单例子
- 20. MyEclipse 6 实战开发讲解视频入门 9 JSF 1.2 入门开发
搜索
*
积分与排名
- 积分 - 632502
- 排名 - 1
最新评论
- 1. re: 如何购买《MyEclipse 6 Java 开发中文教程》DVD光盘(含源代码,视频和软件)及网上答疑
- 评论内容较长,点击标题查看
- --jerryhu
- 2. re: 如何购买《MyEclipse 6 Java 开发中文教程》DVD光盘(含源代码,视频和软件)及网上答疑
- 建议增加spring+jdbc介绍另外JPA部分也增加与spring整和的介绍
- --建议
- 3. re: 电子书 《MyEclipse 6 Java 开发中文教程》 第十章 开发 Spring 应用 完工
- 评论内容较长,点击标题查看
- --BeanSoft
- 4. re: 老军医讲牙石
- 正在想最近总是牙出血是什么原因呢,原来如此!
- --爱上鸟的鱼
- 5. re: 老军医讲牙石
- 正在向最近总是牙出血是什么原因呢,原来如此!
- --爱上鸟的鱼
- 6. re: 原创电子书 《MyEclipse 6 Java 开发中文教程》下载[未登录]
- 评论内容较长,点击标题查看
- --什么捷
- 7. re: 电子书 《MyEclipse 6 Java 开发中文教程》 第十章 开发 Spring 应用 完工
- 评论内容较长,点击标题查看
- --爱上鸟的鱼
- 8. re: MyEclipse 6 实战开发讲解视频入门 6 Web 入门开发 - JSP/HTML/JDBC 登录
- 美国就是变态!怎么搞的!下你个东西,也是左搞搞右搞搞!
- --pzxy
- 9. re: 原创歌曲: Java 就是好呀
- 没发现,老师您这么有喜剧天份
- --tott
- 10. re: MyEclipse 6 实战开发讲解视频入门 7 Struts 入门开发
- 正好,我急用学struts1.0.thanks
- --张晓云
阅读排行榜
- 1. XLoadTree 基于AJAX + XML动态加载的JS树组件的文档翻译- 已完成(原创)(5081)
- 2. MyEclipse Hibernate 快速入门中文版(4417)
- 3. Sysdeo Eclipse Tomcat 3.2.1 插件中文版下载(4393)
- 4. MyEclipse 5.5 开发 Spring + Struts + Hibernate 的详解视频(长1.5小时)(4341)
- 5. 《jsf入门》简体中文版.rar 电子书下载(4100)
- 6. JSP 中 AJAX 的表单提交中文问题的简单解决方案 - GBK 版本(原创)(3942)
- 7. JSP 文件下载的相对完整代码(解决中文问题和Weblogic报错)(3807)
- 8. 北京市IT公司黑名单(转载)(3707)
- 9. AJAX 入门培训 PPT 及示例代码(3651)
- 10. Jigloo 开发 SWT 的入门教程(3626)
- 11. Eclipse 插件精选介绍(原创)(3612)
- 12. Tomcat 5.5 的 Apache Tomcat Native library(3557)
- 13. JSP 中 AJAX 的表单提交中文问题的简单解决方案(3536)
- 14. MySQL 5 绿色版(BAT版本)(3515)
- 15. MyEclipse 6 学习视频集中下载(文档,视频,源代码)(3470)
- 16. LambdaProbe 中文包下载(99%完成)(3434)
- 17. 用开源软件搭建企业内部协作平台, Kill QQ MSN(3391)
- 18. 理解并使用 JSPWiki 中的权限控制(3299)
- 19. 使用 JDK 6 中的 JConsole 监控应用(原创)(3289)
- 20. Netbeans 5.5 + JPA + Hibernate 3 + Tomcat 实例有声视频(3173)
60天内阅读排行
- 1. 翻译: MyEclipse Spring 入门教程(含官方视频和AOP例子)(2239)
- 2. 翻译: MyEclipse AJAX 调试教程(含官方视频)(2203)
- 3. 原创电子书 《MyEclipse 6 Java 开发中文教程》下载(2063)
- 4. Spring 2.5 标注开发的简单例子(2030)
- 5. 翻译: MyEclipse Hibernate 入门教程(含官方视频)(1966)
- 6. 击破谎言: Spring 2.5 并非"完全"支持基于标注的配置(1914)
- 7. MyEclipse 6 实战开发讲解视频入门 6 Web 入门开发 - JSP/HTML/JDBC 登录(1668)
- 8. MyEclipse 6 实战开发讲解视频入门 7 Struts 入门开发(1644)
- 9. MyEclipse 6 实战开发讲解视频入门 9 JSF 1.2 入门开发(1554)
- 10. MyEclipse 6 实战开发讲解视频入门 10 JSP 文件上传下载(1424)
- 11. MyEclipse 6 实战开发讲解视频入门 8 XFire Web Service 入门开发(1369)
- 12. 翻译: MyEclipse Struts 1.x 教程(PDF格式)(1351)
- 13. Spring 1.2和2.0的简单AOP例子(1253)
- 14. 电子书 《MyEclipse 6 Java 开发中文教程》 第七章 Hibernate 开发预览(1174)
- 15. 电子书 《MyEclipse 6 Java 开发中文教程》 第五,六章预览(1165)
- 16. 电子书 《MyEclipse 6 Java 开发中文教程》 第十章 开发 Spring 应用 完工(1101)
- 17. MyEclipse 6 实战开发讲解视频入门 11 Struts 文件上传(1099)
- 18. 电子书 《MyEclipse 6 Java 开发中文教程》 第九章 开发Struts 1.x应用 完工(961)
- 19. 视频讲解: Eclipse/MyEclipse 如何导入项目源代码(900)
- 20. 感谢 2007 年 12 月的 <<程序员>> 杂志推荐我的 Blog(736)
2005年12月5日星期一
基于 Apache DBCP 的数据库连接获取类, 可以让你在 Tomcat 之外的 J2SE 程序或者其它应用服务器上使用 Apache 的数据库连接池.
TODO: 增加最大连接数和最小连接数的设置功能
配置文件:
ConnectionFactory.properties
/# 2004-12-30 /# 数据库连接工厂的配置文件, 类文件参见 util.ConnectionFactory /# 调试标志, 为 true 时使用 JDBC 直接连接(使用 url, driver, user, password 四个参数) /# 为 false 时使用内置数据库连接池(使用参数 jdbc.jndi), 配置文件位于 db.properties 中 debug=false /# JDBC的连接地址 URL jdbc.url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=yuelao /# JDBC 的驱动 jdbc.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver /# JDBC 的连接用户名 jdbc.user=sa /# JDBC 的连接密码 jdbc.password= /#debug 为 false 时, 将根据连接池名获取数据库连接, 适用于程序在服务器运行的时候
.Java 文件:
// / @(/#)ConnectionFactory.java 1.2 2005-11-25 / / Copyright 2005 BeanSoft Studio. All rights reserved. / PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. //
package beansoft.util;
import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties;
import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource;
import beansoft.jsp.StringUtil; import beansoft.sql.DatabaseUtil;
/// / ConnectionFactory provide a factory class that produces all database / connections from here, and it provides methods for shutdown and restart / data sources as well as reading and saving configuration parameters from/to file. / / 数据库连接工厂类, 所有的数据库连接都从这里产生, 提供关闭和重启数据源的方法, 以及读取和保存 / 配置参数到文件的能力. / / 2005-08-19 / Using Apache DBCP as database connection pool provider. / 使用 Apache DBCP 作为连接池的提供类. / / @link http://jakarta.apache.org/commons/dbcp/ / / Dependency: / commons-collections.jar / commons-pool.jar / commons-dbcp.jar / j2ee.jar (for the javax.sql classes) / / If you using this class with Tomcat's web application, then all the above jars / not need to be added because the Tomcat it self has included these class libs. / 如果你在 Tomcat 的 Web Application 中调用这个类, 以上的 JAR 都不用单独 / 加入的, 因为 Tomcat 默认已经自带了这些类库. / / @author BeanSoft / @version 1.2 / 2005-11-25 // public class ConnectionFactory {
/// Database password /*/ private static String password;
/// Database username /*/ private static String user;
/// JDBC URL /*/ private static String url;
/// / JDBC driver class name // private static String driver;
/// / DEBUG flag, default value is true, returns a connection that directly fetched using / JDBC API; if falg is false, returns a connection returned by the connection pool, / if u want depoly the application, please make this flag be false. / 调试标记, 默认值为 true, 则返回直接使用 JDBC 获取的连接; 如果标记为 false, / 则从连接池中返回器连接, 发布程序时请将这个标志 设置为 false. // private static boolean DEBUG = true;
/// Connection properties value /*/ private static Properties props = new Properties();
/// The data source object, added at 2005-08-19 /*/ private static DataSource dataSource = null;
// Load configuration from resource /ConnectionFactory.properties static { loadConfiguration(); }
private ConnectionFactory() { }
/// / Factory method: obtain a database connection. / 工厂方法: 获取一个数据库连接. / / / @return Connection a java.sql.Connection object // public static Connection getConnection() { try { Connection conn = null;
// Debug mode, obtain connection directly through JDBC API if (DEBUG) {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password); } else { // TODO // // Looking data source through JNDI tree // DataSource dataSource = (DataSource) getInitialContext() // .lookup(jndi); // conn = dataSource.getConnection(); conn = setupDataSource().getConnection(); }
return conn; } catch (Exception ex) { System.err.println("Error: Unable to get a connection: " + ex); ex.printStackTrace(); }
return null; }
/// / Load and parse configuration. // public static void loadConfiguration() { try { props.load(new ByteArrayInputStream(readConfigurationString().getBytes()));
// Load DEBUG flag, default to true DEBUG = Boolean.valueOf(props.getProperty("debug", "true")) .booleanValue(); password = props.getProperty("jdbc.password", null); user = props.getProperty("jdbc.user", null); url = props.getProperty("jdbc.url", null); driver = props.getProperty("jdbc.driver"); } catch (Exception e) { e.printStackTrace(); } }
/// / Save the current configuration properties. // public static void saveConfiguration() { saveConfiguration(getProperties()); }
/// / Read content string from configuration file. Because Class.getResourceAsStream(String) / sometimes cache the contents, so here used this method. / 读取配置文件中的字符串. / 因为 Class 类的 getResourceAsStream(String) 方法有时候会出现缓存, 因此 / 不得已使用了这种办法. / @return String, null if failed /*/ public static String readConfigurationString() { try { java.io.FileInputStream fin = new java.io.FileInputStream( getConfigurationFilePath()); java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
int data; while( (data = fin.read()) != -1) { bout.write(data); } bout.close(); fin.close();
return bout.toString(); } catch (Exception ex) { System.err.println("Unable to load ConnectionFactory.properties:" + ex.getMessage()); ex.printStackTrace(); } return null; }
/// / Get the configuration file's real physical path. // private static String getConfigurationFilePath() { return StringUtil.getRealFilePath("/ConnectionFactory.properties"); }
/// / Save string content of a java.util.Properties object. / 保存配置文件中的字符串. / / @param props configuration string / @return operation result // protected static boolean saveConfigurationString(String props) { if(props == null || props.length() <= 0) return false; try { FileWriter out = new FileWriter(getConfigurationFilePath()); out.write(props); out.close();
return true; } catch (Exception ex) { System.err.println("Unable save configuration string to ConnectionFactory.properties:"
- ex); ex.printStackTrace(); }
return false; }
/// / Returns the current database connection properties. / @return Properties object /*/ public static Properties getProperties() { return props; }
/// / Save configuration properties. / / @param props Properties / @return operation result /*/ public static boolean saveConfiguration(Properties props) { if(props == null || props.size() <= 0) return false;
try { FileOutputStream out = new FileOutputStream(getConfigurationFilePath()); props.store(out, ""); out.close();
return true; } catch (Exception ex) { System.err.println("Unable to save ConnectionFactory.properties:" + ex.getMessage()); ex.printStackTrace(); }
return false; }
/// / Create a DataSource instance based on the Apache DBCP. / 创建基于 Apache DBCP 的 DataSource. / / @return a poolable DataSource /*/ public static DataSource setupDataSource() { if(dataSource == null) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUsername(user); ds.setPassword(password); ds.setUrl(url);
dataSource = ds; }
return dataSource; }
/// / Display connection status of current data source. / / 显示当前数据源的状态. // public static String getDataSourceStats() { BasicDataSource bds = (BasicDataSource) setupDataSource(); StringBuffer info = new StringBuffer();
info.append("Active connection numbers: " + bds.getNumActive()); info.append("\n"); info.append("Idle connection numbers: " + bds.getNumIdle());
return info.toString(); }
/// / Shut down the data source, if want use it again, / please call setupDataSource(). /*/ public static void shutdownDataSource() { BasicDataSource bds = (BasicDataSource) setupDataSource(); try { bds.close(); } catch (SQLException e) { // TODO auto generated try-catch e.printStackTrace(); } }
/// / Restart the data source. / 重新启动数据源. /*/ public static void restartDataSource() { shutdownDataSource(); setupDataSource(); }
/// Test method /*/ public static void main(String[] args) { Connection conn = ConnectionFactory.getConnection(); DatabaseUtil dbUtil = new DatabaseUtil(); dbUtil.setConnection(conn); // try { // java.sql.ResultSet rs = conn.createStatement().executeQuery( // "SELECT MAX(ID) FROM items"); // // while(rs.next()) { // System.out.println(rs.getString(1)); // } // // rs.close(); // // } catch (Exception ex) { // ex.printStackTrace(); // } System.out.println(dbUtil.getAllCount("SELECT MAX(ID) FROM items"));
System.out.println(conn);
try { conn.close(); } catch (Exception ex) { // ex.printStackTrace(); }
conn = ConnectionFactory.getConnection();
System.out.println(conn);
try { conn.close(); } catch (Exception ex) { // ex.printStackTrace(); }
System.exit(0); }
} posted on 2007-01-19 11:04 BeanSoft 阅读(2136) 评论(1) 编辑 收藏 所属分类: Database
[Comments
- /# re: 基于 Apache DBCP 的数据库连接获取类(原创)小车马 Posted @ 2007-01-23 23:38 恩,以前也用过,apache的很多东西都非常不错, 楼主,潜力贴论坛(http://content.uu1001.com/)是我个人的一个设想,如果你对java非常的专注,并且愿意交我这个朋友,可以发邮件给我(lbw070105@gmail.com),希望我们可以一起发展它。 回复 更多评论 ]() 刷新评论列表
标题 请输入标题 姓名 请输入你的姓名 主页 请输入验证码 验证码 /* 内容(请不要发表任何与政治相关的内容) 请输入评论内容 Remember Me? 登录 使用高级评论 新用户注册 返回页首 恢复上次提交 [使用Ctrl+Enter键可以直接提交] 该文被作者在 2007-09-22 12:56 编辑过 相关文章:
- 基于JDBC_2.0驱动的分页代码实现
- BeanSoft MySQL Java 开发套装(服务器,管理工具,JDBC驱动,示例代码) 无中文问题
- SQL中主键和外键的区别?(zz)
- 常用 JDBC 驱动名字和 URL 列表
- 如何连接SQL Server数据库(Tomcat 连接池配置) (转载)
- Oracle Sequence 和 java.sql.PreparedStatement 的协同使用
- 学习 SQL 语法的好资料: Transact-SQL 参考
- Hibernate 如何配置显示生成的 SQL?
- Mysql 优化的资料(收集转载)
- MySQL 5 绿色版(BAT版本)
Powered by: BlogJava Copyright © BeanSoft