日期:2014-05-16  浏览次数:20951 次

Tomcat6 下 MySQL 5.1 数据源配制

数据源配制中有一些参数,不太明白,不明白的咱不说.在这说两种我用过的.

?

首先在apache-tomcat-6.0.16\lib? 路径下的? mysql JDBC 5.1? 驱动不能少.

?

第一种是在TomCat 里配制apache-tomcat-6.0.16\conf\context.xml 如下:

<?xml version='1.0' encoding='utf-8'?>
<Context>
	<Resource name="jdbc/myTest" auth="Container"
		type="javax.sql.DataSource" username="root" password=""
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://192.168.0.68:3306/points" />
	<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

?name 是数据源名称.格式是:"jdbc/数据源名称"

username,password 不用说了.这里password 密码为空.

url 和 JDBC 驱动配相同.

?

?

第二种是在项目里面配.WebRoot/META-INF/context.xml? 中配:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource name="jdbc/test" auth="Container"
		type="javax.sql.DataSource" username="root" password=""
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://192.168.0.68:3306/points" />
</Context>

和在 Tomcat 里面几乎一样. context.xml? 要注意是在 META-INF 下新建 是自己新建的.

?

?

数据源的使用:

list = new ArrayList();
Connection conn = null;
DataSource ds = null;
PreparedStatement pst = null;
ResultSet rst = null;
String sqlStr = "insert into `user`(`userNo`,`userName`) values (?,?)";
try {
	InitialContext ctx = new InitialContext();
	ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");
	conn = ds.getConnection();
	pst = conn.prepareStatement(sqlStr);
	pst.clearBatch();
	for (int i = 0; i < 4; i++) {
		pst.setString(1, "good " + i);
		pst.setString(2, "isw " + i);
		// 使用batch,将多个sql操作作为一个单元传输给数据库,
		// Hibernate 中 jdbc.batch_size 网上推荐 30
		pst.addBatch();
	}
	pst.executeBatch();
	rst = pst.executeQuery("select * from user");
	// Fetch Size 是设定JDBC的prepareStatement读取数据的时候每次从数据库中取出的记录条数,
	// Hibernate 中 jdbc.fetch_size 网上推荐 50
	rst.setFetchSize(50);
	while (rst.next()) {
		System.out.println(rst.getString(3));
		list.add(rst.getString(3));
	}
} catch (Exception e) {
	e.printStackTrace();
	return ERROR;
} finally {
	try {
		rst.close();
		pst.close();
		conn.close();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}

? ? 上面说一下,大家都说Hibernate 执行数据库操作要比手动JDBC 效率要高,这是因为它对查询进行了一些优化,Fetch Size,Batch 也许只是冰山一角.说到这里就提了一下它们的用法.

?

关于 Fectch Size,Batch 下面的博客给出较详细的说明.

http://xuganggogo.iteye.com/blog/440516

?

数据源在Hibernate 中的使用? hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
	<session-factory>
                <!-- 下面是数据源的配制  -->
		<property name="connection.datasource">
			java:comp/env/jdbc/test
		</property>
                <!-- 数据库方言 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="jdbc.batch_size">25</property>
		<property name="jdbc.fetch_size">50</property>
		<mapping resource="com/isw2/entity/UserBean.hbm.xml" />
	</session-factory>
</hibernate-configuration>
?

?

人生之所以的许许多多的半途而废及心有不甘,不过因为我们没有坚持自己的理想罢了,只要我们不放弃,理想终会实现.

?

?

加油!坚持.追随我心.