jboss配置mysql数据库连接池实例
1:配置:
JDK1.5
JBoss4.0.4
Mysql5.0
Myeclipse4.1
2:建立数据库:
createdatabasetest;
usetest;
DROPTABLEIFEXISTS`test`;
CREATETABLE`test`(
`Test_id`int(11)NOTNULLauto_increment,
`Test_name`varchar(45)NOTNULLdefault'',
`Test_password`varchar(45)NOTNULLdefault'',
PRIMARYKEY(`Test_id`)
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
INSERTINTO`test`VALUES(1,'test','test'),(2,'test2','test');
3:CopyMySQL的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\jcacopy修改
想配置多个连接池只要多加一个<local-tx-datasource></local-tx-datasource>
其中内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!--$Id:mysql-ds.xml,v1.3.2.32006/02/0714:23:00acoliverExp$-->
<!--DatasourceconfigforMySQLusing3.0.9availablefrom:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<local-tx-datasource>
<jndi-name>test</jndi-name>
<connection-url>jdbc:mysql://127.0.0.1:3306/test</connection-url><!?test为数据库名-->
<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>
<!--shouldonlybeusedondriversafter3.22.1with"ping"support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!--sqltocallwhenconnectioniscreated
<new-connection-sql>somearbitrarysql</new-connection-sql>
-->
<!--sqltocallonanexistingpooledconnectionwhenitisobtainedfrompool-MySQLValidConnectionCheckerispreferredfornewerdrivers
<check-valid-connection-sql>somearbitrarysql</check-valid-connection-sql>
-->
<!--correspondingtype-mappinginthestandardjbosscmp-jdbc.xml(optional)-->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
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-policyname="MySqlDbRealm">
<authentication>
<login-modulecode=
"org.jboss.resource.security.ConfiguredIdentityLoginModule"
flag="required">
<module-optionname="principal">test</module-option>
<module-optionname="userName">root</module-option>
<module-optionname="password">xxxxxxxx</module-option>
<module-optionname="managedConnectionFactoryName">
jboss.jca:service=LocalTxCM,name=test
</module-option>
</login-module>
</authentication>
</application-policy>
6:Myeclispe新建Webproject命名为:UseTest
新建JAVA类DatabaseConn.java
packagecom.db;
importjava.sql.*;
importjavax.naming.*;
importjavax.sql.DataSource;
publicclassDatabaseConn{
publicstaticsynchronizedConnectiongetConnection(){
try{
ContextenvCtx=newInitialContext();
DataSourceds=(DataSource)envCtx.lookup("java:/test");
returnds.getConnection();
}catch(SQLExceptione){
System.out.println("数据源配置发生错误"+e.toString());
returnnull;
}catch(NamingExceptione2){
System.out.print("数据源配置"+e2.toString());
returnnull;
}
}
publicstaticvoidclose(ResultSetrs,Statementst,Connectionconn){
try{
if(rs!=null)
rs.close();
}catch(SQLExceptionex){
}
;
try{
if(st!=null)
st.close();
}catch(SQLExceptionex){
}
;
try{
if(conn!=null)
conn.close();
}catch(SQLExceptionex){
}
;
}
}
7:新建JSP页面:MyJsp.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB2312"%>
<%@pageimport="java.sql.*"%>
<%@pageimport="com.db.*"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>MyJSP'MyJsp.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<%
Connectionconn=DatabaseConn.getConnection();
Statementstmt=conn.createStatement();
ResultSetrs=stmt.executeQuery("select*fromtest");
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:部署Webproject
9:重新启动服务器
10:访问:http://127.0.0.1:8080/UseTest/MyJsp.jsp