一、请将classes12.zip加入classpath中去。
二、请参况hsqldb-service.xml文件编写Oracledb-service.xml文件
请打开server\default\deploy目录下的hsqldb-service.xml文件,这就是一个Jboss3.0_Tomcat4.03下实现数据库连接的例子,它是连接hsql数据库的它的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- $Id: hsqldb-service.xml,v 1.2.2.5 2002/06/23 21:25:58 d_jencks Exp $ -->
<server>
<!-- ==================================================================== -->
<!-- New ConnectionManager setup for default hsql dbs -->
<!-- Build jmx-api (build/build.sh all) and view for config documentation -->
<!-- ==================================================================== -->
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=hsqldbDS">
<!-- Include a login module configuration named HsqlDbRealm.
Update your login-conf.xml, here is an example for a
ConfiguredIdentityLoginModule:
<application-policy name = "HsqlDbRealm">
<authentication>
<login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required">
<module-option name = "principal">sa</module-option>
<module-option name = "userName">sa</module-option>
<module-option name = "password"></module-option>
<module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=hsqldbDS</module-option>
</login-module>
</authentication>
</application-policy>
NOTE: the application-policy name attribute must match
SecurityDomainJndiName, and the
module-option name = "managedConnectionFactoryName"
must match the object name of the ConnectionManager you are configuring here.
-->
<!--uncomment out this line if you are using the DB2DbRealm above
<attribute name="SecurityDomainJndiName">HsqlDbRealm</attribute>
-->
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=hsqldbDS">
<attribute name="JndiName">DefaultDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:hsqldb:hsql://localhost:1476</config-property>
<config-property name="DriverClass" type="java.lang.String">org.hsqldb.jdbcDriver</config-property>
<config-property name="UserName" type="java.lang.String">sa</config-property>
<config-property name="Password" type="java.lang.String"></config-property>
</properties>
</attribute>
<!--Below here are advanced properties -->
<!--hack-->
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
<depends>jboss:service=Hypersonic</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=hsqldbDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<!--criteria indicates if Subject (from security domain) or app supplied
parameters (such as from getConnection(user, pw)) are used to distinguish
connections in the pool. Choices are
ByContainerAndApplication (use both),
ByContainer (use Subject),
ByApplication (use app supplied params only),
ByNothing (all connections are equivalent, usually if adapter supports
reauthentication)-->
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment-->
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
<!-- Moved to end to test anonymous depends -->
<mbean code="org.jboss.jdbc.HypersonicDatabase"
name="jboss:service=Hypersonic">
<attribute name="Port">1476</attribute>
<attribute name="Silent">true</attribute>
<attribute name="Database">default</attribute>
<attribute name="Trace">false</attribute>
</mbean>
</server>
在<!-- -->之间没有用的一些说明,所以去掉这一些说明后经过改写一个存取oracle数据库的服务的XML的文件是Oracledb-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=oracleDS">
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=oracleDS">
<attribute name="JndiName">OracleDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:oracle:thin:@cuipeng:1521:ora8</config-property>
<config-property name="DriverClass" type="java.lang.String">oracle.jdbc.driver.OracleDriver</config-property>
<config-property name="UserName" type="java.lang.String">system</config-property>
<config-property name="Password" type="java.lang.String">manager</config-property>
</properties>
</attribute>
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=oracleDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
</server>
请大家对比两个文件的不同之外,很容易写出其它数据库的存取服务了,主要的是下面这个部分
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=oracleDS">
<attribute name="JndiName">OracleDS</attribute><!--这个地方是JNDI的名字在ejb或jsp中调用的是java:/OracleDS - ->
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:oracle:thin:@cuipeng:1521:ora8</config-property>
<config-property name="DriverClass" type="java.lang.String">oracle.jdbc.driver.OracleDriver</config-property>
<config-property name="UserName" type="java.lang.String">system</config-property>
<config-property name="Password" type="java.lang.String">manager</config-property>
</properties>
</attribute>
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
还有就是各个部分的名字了name=oracleDS
三、测试数据的连接列子(我用的是jsp)
你必需要编写.war或.ear包来测试一个简易的做法就是:
一、在deploy目录下建立oracle-test.war目录。
二、在oracle-test.war目录下建立WEB-INF目录,其中有两个文件
jboss-web.xml和web.xml
内容分别是
jboss-web.xml内容是
<jboss-web>
</jboss-web>
web.xml内容是
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>
三、在oracle-test.war目录下建立新文件index.jsp其内容是
<%@page contentType="text/html"%>
<%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
<h3>Test Oracle DB</h3>
<%
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/OracleDS"); //注意这里哦和前面对应
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TAB");
while ( rs.next() ) {
out.println( rs.getString("TNAME") + "<br>");
}
conn.close();
%>
四、请访问http://localhost:8080/oracle-test
一切都测试OK的,如果有什么不对的地方请指哦,我也是学习的。
SQLSERVER数据库的测试
在deploy目录下建立SqlServerDB-service.xml文件。
其内容如下
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=sqlServerDS">
<depends optional-attribute-name="ManagedConnectionFactoryName">
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=sqlServerDS">
<attribute name="JndiName">SqlServerDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:microsoft:sqlserver://asen:1433;DatabaseName=infomag</config-property>
<config-property name="DriverClass" type="java.lang.String">com.microsoft.jdbc.sqlserver.SQLServerDriver</config-property>
<config-property name="UserName" type="java.lang.String">sa</config-property>
<config-property name="Password" type="java.lang.String">bodaasen</config-property>
</properties>
</attribute>
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=sqlServerDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
</server>
三、测试数据的连接列子(我用的是jsp)
你必需要编写.war或.ear包来测试一个简易的做法就是:
一、 在deploy目录下建立sqlserver-test.war目录。
二、在sqlserver-test.war目录下建立WEB-INF目录,其中有两个文件
jboss-web.xml和web.xml
内容分别是
jboss-web.xml内容是
<jboss-web>
</jboss-web>
web.xml内容是
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>
三、在sqlserver-test.war目录下建立新文件index.jsp其内容是
<%@page contentType="text/html"%>
<%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
<h3>Test SQL SERVER DB</h3>
<%
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/SqlServerDS");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user_infor");
while ( rs.next() ) {
out.println( rs.getString(1) + "");
}
conn.close();
%>
http://localhost:8080/sqlserver-test
一切OK
结果是
Test SQL SERVER DB
001
002
003
admin