以jboss7.1.0和mysql为例
1、找到安装目录,我的目录是:jboss-as-7.1.0.CR1b
2、找到standalone.xml,这个文件在我的电脑上是C:\Users\dan\Desktop\jboss-as-7.1.0.CR1b\standalone\configuration
3、打开,并搜索datasources
4、进行编辑
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS_Pooll"
enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/DBName</connection-url>
<driver>mysql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>10</max-pool-size>
<prefill>true</prefill>
<use-strict-min></use-strict-min>
</pool>
<security>
<user-name>root</user-name>
<password>123456</password>
</security>
<statement>
<track-statements>true</track-statements>
<prepared-statement-cache-size>10</prepared-statement-cache-size>
</statement>
<timeout>
<idle-timeout-minutes>10</idle-timeout-minutes>
<query-timeout>600</query-timeout>
</timeout>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class> <xa-datasource-class>com.mysql.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
5、到E:\jboss-as-7.1.0.CR1b\modules\com文件夹下面新建一个mysql,在进入新建一个main文件夹在进入,把数据库.jar放进去
6、在这个E:\jboss-as-7.1.0.CR1b\modules\com\mysql\main文件夹里面新建一个module.xml
7、打开module.xml进行编辑
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.0.5-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
切记:path是和你的数据库jar一样的
8、到web中使用
<%
Context c=new InitialContext();
DataSource dataSource=(DataSource) c.lookup("java:jboss/datasources/ExampleDS");
Connection conn=dataSource.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
try{
pstm=conn.prepareStatement("select *from score");
rs=pstm.executeQuery();
while(rs.next()){
out.println(rs.getInt(1)+"<br>");
}
}catch(Exception e){}
%>