在Eclipse中使用JBoss數(shù)據(jù)庫連接池技術(shù)
配置:JBoss 5.0 Eclipse Platform3.4 MySQL4.0
在Eclipse中使用JDBC橋來連接數(shù)據(jù)庫大家應(yīng)該都很熟悉,今天我要說的是如何在Eclipse中使用數(shù)據(jù)庫連接池技術(shù)。
為何要使用連接池,使用數(shù)據(jù)庫連接池有什么好處?這些請google一下。
1. 新建一個project
這一步就很簡單了,就是新建一個Java Project。
2. 導(dǎo)入相應(yīng)的jar包
在本例中要使用JBoss和MySQL,所以要導(dǎo)入JBoss的所有包(在你的JBoss目錄下的lib文件夾里)和Java連接MySQL的jar包(mysql-connector-java-5.0.5-bin.jar)
3. 配置MySQL數(shù)據(jù)庫
在MySQL數(shù)據(jù)庫test中新建一個表,名為student,有三個字段名,分別為id,name和age

可以使用GUI來創(chuàng)建。
4. 配置JBoss服務(wù)器
配置JBoss服務(wù)器是最重要的一步,也是最難得一步
(1) 把Java連接MySQL的jar包(mysql-connector-java-5.0.5-bin.jar)拷到JBoss目錄下\server\default\lib里

(2) 編寫mssql-ds.xml配置文件,這個類似的文件可以在JBoss的例子程序中找到,我們修改一下就可以了(附修改后的mssql-ds.xml文件),在拷到JBoss目錄下\server\default\deploy里

<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- $Id: mssql-ds.xml 23720 2004-09-15 14:37:40Z loubyansky $ -->
<!-- ======================================================================-->
<!-- New ConnectionManager setup for Microsoft SQL Server 2000 driver -->
<!-- You may download the latest Microsoft JDBC driver from *Microsoft* -->
<!-- http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/MSDN-FILES/027/001/779/msdncompositedoc.xml&frame=true -->
<!-- ===================================================================== -->
<datasources>
<local-tx-datasource>
<jndi-name>MysqlDS</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>lishunli</password>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>MySQL 5.0</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
大家可以參考一下,注意其中的<use-java-context>false</use-java-context>
語句,具體為什么這樣寫,上網(wǎng)搜搜。
5. 編寫代碼(JDBCPoolDemo.java)
/* 導(dǎo)入必要的包 */
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
public class JDBCPoolDemo
{
public static void main(String args[])
{
String tablename="student"; //數(shù)據(jù)庫中表名
String sqlstr; //sql語句
Connection con=null; //連接對象
Statement stmt=null; //語句對象
ResultSet rs=null; //結(jié)果集對象
Context ctx=null;
Hashtable ht=new Hashtable();
try
{
/*1、建立數(shù)據(jù)庫連接 */
ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
ht.put(Context.PROVIDER_URL,"localhost:1099");
ht.put(Context.URL_PKG_PREFIXES,"org.jboss.naming");
// 創(chuàng)建一個初始上下文環(huán)境
ctx=new InitialContext(ht);
DataSource ds=(DataSource)ctx.lookup("MysqlDS");
//利用DataSource調(diào)用getConnection()方法,獲取數(shù)據(jù)庫的配置信息。
con=ds.getConnection();
/*2、向數(shù)據(jù)庫提交查詢請求 */
stmt=con.createStatement(); // 創(chuàng)建statement對象
sqlstr="select * from "+tablename; // 書寫SQL語句
rs=stmt.executeQuery(sqlstr); // 執(zhí)行SQL語句,返回查詢結(jié)果
/*3、讀取查詢結(jié)果 */
while(rs.next())
{
System.out.print(rs.getString("id"));
System.out.print("\t");
System.out.print(rs.getString("name"));
System.out.print("\t");
System.out.print(rs.getInt("age"));
System.out.print("\t");
System.out.print("\n");
}
}
/*4、異常處理 */
catch(NamingException e1)
{
System.out.println(e1.toString());
System.out.println("驅(qū)動程序沒有找到!");
}
catch(SQLException e2)
{
System.out.println(e2.toString());
System.out.println("數(shù)據(jù)庫異常!");
}
/*5、關(guān)閉數(shù)據(jù)庫 */
finally
{
try
{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
}
6. 調(diào)試運(yùn)行
打開JBoss服務(wù)器,在JNDI樹下應(yīng)該有個MysqlDS 的JNDI,運(yùn)行程序。

有什么問題,請Q我(506817493)
木子寫于2009年3月30日
博客中的一些下載已經(jīng)放到了百度云了,請根據(jù)需要下載。【點(diǎn)我去百度云下載】
最后弱弱地說一下,如果可以的話,轉(zhuǎn)載請?zhí)峁┏鎏?
),謝謝。
posted on 2010-01-07 20:46
李順利 閱讀(1087)
評論(0) 編輯 收藏 所屬分類:
Tips