Druid DBUtils ORM 封裝

package com.msi.druid;

import java.io.IOException; import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.pool.DruidPooledConnection; import com.msi.model.User;

public class DBHelper {

private static DBHelper dbHlper = null;
private static DruidDataSource druidDataSource;
private DataSource dataSource;
private QueryRunner runner = null;

public static DBHelper getInstance() {
	if (dbHlper == null) {
		dbHlper = new DBHelper();
	}
	return dbHlper;
}

static {
	Properties properties = loadProperties("db_server.properties");
	try {
		druidDataSource = (DruidDataSource) DruidDataSourceFactory
				.createDataSource(properties);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public DruidDataSource getDruidDataSource() {
	return druidDataSource;
}

// 当不支持的时候,显式调用Connection
public DruidPooledConnection getConnection() throws SQLException {
	return druidDataSource.getConnection();
}

public DataSource getDataSource() {
	this.dataSource = DBHelper.getInstance().getDruidDataSource();
	return dataSource;
}

public static Properties loadProperties(String fileName) {
	// TODO Auto-generated method stub
	Properties p = new Properties();

	// 方法1获取配置
	// InputStream inStream =
	// DBHelper.class.getClass().getResourceAsStream("/db_dang.properties");
	InputStream inStream = null;
	try {

// 获取配置的第二种方式 // String path = DBHelper.class.getClassLoader().getResource("\") // .getPath(); // path = new File(path).getParent(); // String profilepath = path + File.separator + fileName; // System.out.println("file ==="+profilepath); // inStream = new FileInputStream(new File(profilepath));

		inStream = DBHelper.class.getClass().getResourceAsStream("/"+fileName);
		p.load(inStream);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		try {
			if (inStream != null) {
				inStream.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	return p;
}

public QueryRunner initRunner() {
	this.dataSource = DBHelper.getInstance().getDataSource();
	if (this.dataSource != null) {
		this.runner = new QueryRunner(this.dataSource);
	}
	return runner;
}

public QueryRunner getRunner() {
	return initRunner();
}

public static void main(String[] args) throws SQLException {
	// TODO Auto-generated method stub
	List<User> userList = DBHelper
			.getInstance()
			.getRunner()
			.query("select * from t_user",
					new BeanListHandler<User>(User.class));
	for (int i = 0; i < userList.size(); i++) {
		System.out.print(userList.get(i) + "\r\n");
	}

	DruidPooledConnection conn = DBHelper.getInstance().getConnection();
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("select * from t_user");
	User user = null;
	while (rs.next()) {
		user = new User();
		user.setUserName(rs.getString("userName"));
		System.err.println(user.getUserName());
	}
}

}

配置如下 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db_diary username=root password=yan filters=stat initialSize=2 maxActive=300 maxWait=60000 timeBetweenEvictionRunsMillis=60000 minEvictableIdleTimeMillis=300000 validationQuery=SELECT 1 testWhileIdle=true testOnBorrow=false testOnReturn=false poolPreparedStatements=true maxPoolPreparedStatementPerConnectionSize=200

WebXml中配置監控功能

<!-- 启用Web监控统计功能需要在Web应用的web.xml中加入这个Servlet声明 -->

<servlet>
	<servlet-name>DruidStatView</servlet-name>
	<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet>
	<description>This is the description of my J2EE component</description>
	<display-name>This is the display name of my J2EE component</display-name>
	<servlet-name>DruidServlet</servlet-name>
	<servlet-class>com.msi.servlet.DruidServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>DruidStatView</servlet-name>
	<url-pattern>/druid/*</url-pattern>
</servlet-mapping>

转载于:https://my.oschina.net/u/946001/blog/269661

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
115le网址导航,版面清晰简洁,并可实现版面自由换色。 演示地址http://www.115le.com/indexcw.html 安装方法 1.上传网站安装程序到空间,空间需支持PHP,MYSQL数据库20M 即可。 2.访问网网址执行安装,按照提示填入mysql数据库信息。 3.安装时默认设置后台管理 用户:admin 密码:123456 (以防安装出错建议默认安装,然后登陆后台修 改密码) 4.网站页面全后台操控,方便简单。安装后即可生成静态页面。 5.由于个别板块显示设置,请进入后台数据管理,导入数据备份文件即可,安装全部结束。 6.重装时请复制install文件夹和index.html文件至根目录执行重装。此版本改动较大不可以用114la的install文件夹进行重装会造成不可预料结果 7.请手动去掉 首页底部模板中的 淘宝客和站长统计代码 8 .其他需要修改的信息都在后台模板中,或在\static\js文件中请自行修改 2010.07.14更新说明。。。 1.更新163.126邮箱不能登录的问题。 2.增加手写功能,方便不会打字的人 3.对网页进行了优化,打开速度明显快了,演示 http://www.115le.com/indexcw.html 4.增加一个娱乐的宠物,闲着没事了还可以给它喂食!嘿嘿 5.增加了飞机,酒店搜索功能,方便旅游出行! 6.底部搜索框改为谷歌搜索。。。。。。。。 115导航网QQ交流群:57830239 下载地址在www.115le.com点网站底部的下载本系统(不断更新)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值