jboss配置mysql数据库连接池实例
:配置:
JDK
JBoss
Mysql
Myeclipse
:建立数据库:
createdatabasetest;
usetest;
DROPTABLEIFEXISTS`test`;
CREATETABLE`test`(
`Test_id`int()NOTNULLauto_increment
`Test_name`varchar()NOTNULLdefault
`Test_password`varchar()NOTNULLdefault
PRIMARYKEY(`Test_id`)
)ENGINE=InnoDBDEFAULTCHARSET=latin;
INSERTINTO`test`VALUES(testtest)(testtest);
:CopyMySQL的JDBC驱动放到jboss\server\default\lib
可到MYsql网站下载
:在jboss\server\default\deploy下新建文件mysqldsxml
可从jboss\docs\examples\jcacopy修改
想配置多个连接池只要多加一个<localtxdatasource></localtxdatasource>
其中内容如下:
<?xmlversion=encoding=UTF?>
<!$Id:mysqldsxmlv//::acoliverExp$>
<!DatasourceconfigforMySQLusingavailablefrom:
l
>
<datasources>
<localtxdatasource>
<jndiname>test</jndiname>
<connectionurl>jdbc:mysql://:/test</connectionurl><!?test为数据库名>
<driverclass>commysqljdbcDriver</driverclass>
<username>root</username><!?用户名以下相同>
<password>xxxxxxxx</password><!?密码以下相同>
<exceptionsorterclassname>orgjbossresourceadapterjdbcvendorMySQLExceptionSorter</exceptionsorterclassname>
<!shouldonlybeusedondriversafterwithpingsupport
<validconnectioncheckerclassname>orgjbossresourceadapterjdbcvendorMySQLValidConnectionChecker</validconnectioncheckerclassname>
>
<!sqltocallwhenconnectioniscreated
<newconnectionsql>somearbitrarysql</newconnectionsql>
>
<!sqltocallonanexistingpooledconnectionwhenitisobtainedfrompoolMySQLValidConnectionCheckerispreferredfornewerdrivers
<checkvalidconnectionsql>somearbitrarysql</checkvalidconnectionsql>
>
<!correspondingtypemappinginthestandardjbosscmpjdbcxml(optional)>
<metadata>
<typemapping>mySQL</typemapping>
</metadata>
</localtxdatasource>
</datasources>
:修改jboss\server\default\conf\standardjawsxml
<jaws>
<datasource>java:/test</datasource>
<typemapping>mySql</typemapping>
</jaws>
修改jboss\server\default\conf\standardjbosscmpjdbcxml
<jbosscmpjdbc>
<defaults>
<datasource>java:/test</datasource>
<datasourcemapping>mySql</datasourcemapping>
</defaults>
</jbosscmpjdbc>
修改jboss\server\default\conf\loginconfigxml
<applicationpolicyname=MySqlDbRealm>
<authentication>
<loginmodulecode=
orgjbossresourcesecurityConfiguredIdentityLoginModule
flag=required>
<moduleoptionname=principal>test</moduleoption>
<moduleoptionname=userName>root</moduleoption>
<moduleoptionname=password>xxxxxxxx</moduleoption>
<moduleoptionname=managedConnectionFactoryName>
jbossjca:service=LocalTxCMname=test
</moduleoption>
</loginmodule>
</authentication>
</applicationpolicy>
:Myeclispe新建Webproject命名为:UseTest
新建JAVA类DatabaseConnjava
packagecomdb;
importjavasql*;
importjavaxnaming*;
importjavaxsqlDataSource;
publicclassDatabaseConn{
publicstaticsynchronizedConnectiongetConnection(){
try{
ContextenvCtx=newInitialContext();
DataSourceds=(DataSource)envCtxlookup(java:/test);
returndsgetConnection();
}catch(SQLExceptione){
Systemoutprintln(数据源配置发生错误+etoString());
returnnull;
}catch(NamingExceptione){
Systemoutprint(数据源配置+etoString());
returnnull;
}
}
publicstaticvoidclose(ResultSetrsStatementstConnectionconn){
try{
if(rs!=null)
rsclose();
}catch(SQLExceptionex){
}
;
try{
if(st!=null)
stclose();
}catch(SQLExceptionex){
}
;
try{
if(conn!=null)
connclose();
}catch(SQLExceptionex){
}
;
}
}
:新建JSP页面:MyJspjsp
<%@pagelanguage=javaimport=javautil*pageEncoding=GB%>
<%@pageimport=javasql*%>
<%@pageimport=comdb*%>
<!DOCTYPEHTMLPUBLIC//WC//DTDHTMLTransitional//EN>
<html>
<head>
<title>MyJSPMyJspjspstartingpage</title>
<metahttpequiv=pragmacontent=nocache>
<metahttpequiv=cachecontrolcontent=nocache>
<metahttpequiv=expirescontent=>
<metahttpequiv=keywordscontent=keywordkeywordkeyword>
<metahttpequiv=descriptioncontent=Thisismypage>
<!
<linkrel=stylesheettype=text/css>
>
</head>
<body>
<%
Connectionconn=DatabaseConngetConnection();
Statementstmt=conncreateStatement();
ResultSetrs=stmtexecuteQuery(select*fromtest);
while(rsnext())
{
outprintln(rsgetInt(Test_id));
outprintln(rsgetString(Test_name));
outprintln(rsgetString(Test_password));
}
DatabaseConnclose(rsstmtconn);
%>
</body>
</html>
:部署Webproject
:重新启动服务器
:访问: