jboss配置 mysql数据库连接池实例 1 :配置: JDK 1.5 + JBoss4.0.4 + Mysql5.0
2:建立数据库:create database test;use test;DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `Test_id` int(11) NOT NULL auto_increment, `Test_name` varchar(45) NOT NULL default '', `Test_password` varchar(45) NOT NULL default '', PRIMARY KEY (`Test_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;INSERT INTO `test` VALUES (1,'test','test'),(2,'test2','test');
3:Copy MySQL的JDBC驱动放到jboss-4.0.4/server/default/lib 可到MYsql网站下载www.mysql.com
4:在 jboss-4.0.4/server/default/deploy下新建文件mysql-ds.xml 可从jboss-4.0.4/docs/examples/jca copy 修改 想配置多个连接池只要多加一个<local-tx-datasource /></local-tx-datasource /> 其中内容如下:
<
jndi-name
/>
test
</
jndi-name /
>
<
connection-url
/>
jdbc:mysql://127.0.0.1:3306/test
</
connection-url /
>
<
driver-class
/>
com.mysql.jdbc.Driver
</
driver-class /
>
<
user-name
/>
root
</
user-name /
>
<
password
/>
xxxxxxxx
</
password /
>
<
exception-sorter-class-name
/>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</
exception-sorter-class-name /
>
<
type-mapping
/>
mySQL
</
type-mapping /
>
5:修改jboss-4.0.4/server/default/conf/ standardjaws.xml
<
jaws
/>
<
datasource
/>
java:/test
</
datasource /
>
<
type-mapping
/>
mySql
</
type-mapping /
>
.....
</
jaws /
>
修改jboss-4.0.4/server/default/conf/ standardjbosscmp-jdbc.xml
<
jbosscmp-jdbc
/>
<
defaults
>
<
datasource
/>
java:/test
</
datasource /
>
<
datasource-mapping
/>
mySql
</
datasource-mapping /
>
</
defaults
></
jbosscmp-jdbc /
>
修改jboss-4.0.4/server/default/conf/ login-config.xml
<
application-policy
name
= "mysqldbrealm"
/>
<
login-module
code
= "org.jboss.resource.security.ConfiguredIdentityLoginModule"
flag
= "required"
>
<
module-option
name
="principal"
/>
test
</
module-option /
>
<
module-option
name
="username"
/>
root
</
module-option /
>
<
module-option
name
="password"
/>
xxxxxxxx
</
module-option /
>
<
module-option
name
="managedconnectionfactoryname"
/>
jboss.jca:service=LocalTxCM,name=test
</
module-option /
>
6:Myeclispe 新建Web project 命名为:UseTest
DatabaseConn.javapackage com.db;
import
java.sql.
*
;
import
javax.naming.
*
;
import
javax.sql.DataSource;
public
class
DatabaseConn
...
{ public static synchronized Connection getConnection() ... { try ... { Context envCtx = new InitialContext(); DataSource ds = (DataSource) envCtx.lookup( " java:/test " ); return ds.getConnection(); } catch (SQLException e) ... { System.out.println( " 数据源配置发生错误 " + e.toString()); return null ; } catch (NamingException e2) ... { System.out.print( " 数据源配置 " + e2.toString()); return null ; } } public static void close(ResultSet rs, Statement st, Connection conn) ... { try ... { if (rs != null ) rs.close(); } catch (SQLException ex) ... { } ; try ... { if (st != null ) st.close(); } catch (SQLException ex) ... { } ; try ... { if (conn != null ) conn.close(); } catch (SQLException ex) ... { } ; } }
7:新建JSP页面:MyJsp.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
GB2312
"
%>
<%
@ page
import
=
"
java.sql.*
"
%>
<%
@ page
import
=
"
com.db.*
"
%>
<!
DOCTYPE HTML PUBLIC
"
-//W3C//DTD HTML 4.01 Transitional//EN
"
>
<
html
>
<
head
>
<
title
>
My JSP
'
MyJsp.jsp
'
starting page
</
title
>
<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
meta http
-
equiv
=
"
keywords
"
content
=
"
keyword1,keyword2,keyword3
"
>
<
meta http
-
equiv
=
"
description
"
content
=
"
This is my page
"
>
<!--
<
link rel
=
"
stylesheet
"
type
=
"
text/css
"
href
=
"
styles.css
"
>
-->
</
head
>
<
body
>
<%
Connection conn
=
DatabaseConn.getConnection(); Statement stmt
=
conn.createStatement(); ResultSet rs
=
stmt.executeQuery(
"
select * from test
"
);
while
(rs.next())
...
{ out.println(rs.getInt( " Test_id " )); out.println(rs.getString( " Test_name " )); out.println(rs.getString( " Test_password " )); }
DatabaseConn.close(rs,stmt,conn);
%>
</
body
>
</
html
>
8 :部署Web project9:重新启动服务器
10:访问: http://127.0.0.1:8080/UseTest/MyJsp.jsp