There are some parameters in the data source configuration that I don’t quite understand. I won’t talk about those that I don’t understand. Here are two that I have used.
First of all, the mysql JDBC 5.1 driver in the apache-tomcat-6.0.16lib path cannot be missing.
The first is to prepare apache-tomcat-6.0.16confcontext.xml in TomCat as follows:
Xml code
<?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>
<?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 is the data source name. The format is: "jdbc/data source name"
Needless to say username, password. The password here is empty.
The url is the same as the JDBC driver configuration.
http://bizhi.knowsky.com/
The second is to configure .WebRoot/META-INF/context.xml in the project:
Xml code
<?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>
<?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>It is almost the same as in Tomcat. Please note that context.xml is created by yourself under META-INF.
Data source usage:
Java code
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);
// Use batch to transfer multiple SQL operations to the database as a unit.
// jdbc.batch_size in Hibernate is recommended online at 30
pst.addBatch();
}
pst.executeBatch();
rst = pst.executeQuery("select * from user");
// Fetch Size is to set the number of records fetched from the database each time when JDBC's prepareStatement reads data.
// jdbc.fetch_size in Hibernate is recommended online at 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();
}
}
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);
// Use batch to transfer multiple SQL operations to the database as a unit.
// jdbc.batch_size in Hibernate is recommended online at 30
pst.addBatch();
}
pst.executeBatch();
rst = pst.executeQuery("select * from user");
// Fetch Size is to set the number of records fetched from the database each time when JDBC's prepareStatement reads data.
// jdbc.fetch_size in Hibernate is recommended online at 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();
}
} Let me talk about it above. Everyone says that Hibernate performs database operations more efficiently than manual JDBC. This is because it has some optimizations for queries. Fetch Size and Batch may be just the tip of the iceberg. I have mentioned their usage here. .
Regarding Fectch Size, the blog below Batch gives a more detailed explanation.
http://xuganggogo.javaeye.com/blog/440516
The use of data sources in Hibernate hibernate.cfg.xml
Xml code
<?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>
<!-- The following is the configuration of the data source -->
<property name="connection.datasource">
java:comp/env/jdbc/test
</property>
<!-- Database Dialect -->
<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>
<?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>
<!-- The following is the configuration of the data source -->
<property name="connection.datasource">
java:comp/env/jdbc/test
</property>
<!-- Database Dialect -->
<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>
The reason why many of us give up halfway and feel unwilling to do so in life is just because we don’t stick to our ideals. As long as we don’t give up, our ideals will eventually come true.