資料來源配製中有一些參數,不太明白,不明白的咱不說.在這說兩種我用過的.
首先在apache-tomcat-6.0.16lib 路徑下的 mysql JDBC 5.1 驅動程式不能少.
第一種是在TomCat 配製apache-tomcat-6.0.16confcontext.xml 如下:
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>
<?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 驅動配相同.
http://bizhi.knowsky.com/
第二種是配在專案裡面.WebRoot/META-INF/context.xml 中配:
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>
<?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 下新建是自己新建的.
資料來源的使用:
Java程式碼
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();
}
}
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.javaeye.com/blog/440516
資料來源在Hibernate 中的使用 hibernate.cfg.xml
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>
<?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>
人生之所以的許許多多的半途而廢及心有不甘,不過因為我們沒有堅持自己的理想罷了,只要我們不放棄,理想終會實現.