这个测试的这个例子中不含EJB内容,只是一个非常简单的数据库连接,因为其中一个细节纠缠了一整天所以记下来权做教训:).
试验的最终目的是这样:
......
Context ctx = new InitialContext(); //具体jndi环境设置在jndi.properties中指定
DataSource ds = (DataSource)ctx.lookup("Zqb Oracle Test"); //其中Zqb Oracle Test是我在oracle-ds.xml中指定的JNDI名
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("select count(1) from css_emp_info");
ResultSet rs = ps.executeQuery();
......
步骤如下:
1,jndi.properties(使用jboss缺省的内容,不做修改):
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
2,将jboss-4.0.1sp1/docs/examples/jca下的oracle-ds.xml拷贝到jboss-4.0.1sp1/server/default/deploy下,并做相应修改,主要内容如下:
<datasources>
<local-tx-datasource>
<jndi-name>Zqb Oracle Test</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:@10.25.10.175:1521:pacesdev</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>cesbase</user-name>
<password>voiture6</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
3,OK,部署war,搞定!其中红色标注的是后面加上的,有了这句在jndi前可以不用加前缀;否则,需要加前缀java(即:lookup("Zqb Oracle Test") 和 lookup("java:Zqb Oracle Test")的区别).
补充: 发现在jboss官方网站有关于use-java-context这个属性的说明 http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources ,仔细看看!
--------------------------------------------------自己的例子--------------------------------------------
oracle-ds.xml
<?
xml version="1.0" encoding="UTF-8"
?>

<!--
=====================================================================
-->
<!--
-->
<!--
JBoss Server Configuration
-->
<!--
-->
<!--
=====================================================================
-->

<!--
$Id: oracle-ds.xml,v 1.6 2004/09/15 14:37:40 loubyansky Exp $
-->
<!--
====================================================================
-->
<!--
Datasource config for Oracle originally from Steven Coy
-->
<!--
====================================================================
-->


<
datasources
>
<
local-tx-datasource
>
<!--
<jndi-name>OracleDS</jndi-name>
-->
<
jndi-name
>
ming
</
jndi-name
>
<
use-java-context
>
false
</
use-java-context
>
<
connection-url
>
jdbc:oracle:thin:@3.242.165.91:1521:GETEST
</
connection-url
>
<!--

Here are a couple of the possible OCI configurations.
For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly.
-->
<
driver-class
>
oracle.jdbc.driver.OracleDriver
</
driver-class
>
<
user-name
>
ming
</
user-name
>
<
password
>
tata123
</
password
>
<!--
Uses the pingDatabase method to check a connection is still valid before handing it out from the pool
-->
<!--
valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name
-->
<!--
Checks the Oracle error codes and messages for fatal errors
-->
<
exception-sorter-class-name
>
org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
</
exception-sorter-class-name
>
<!--
sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->

<!--
sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!--
corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional)
-->
<
metadata
>
<
type-mapping
>
Oracle9i
</
type-mapping
>
</
metadata
>
</
local-tx-datasource
>
<
local-tx-datasource
>
<!--
<jndi-name>OracleDS</jndi-name>
-->
<
jndi-name
>
mingtest
</
jndi-name
>
<
use-java-context
>
false
</
use-java-context
>
<
connection-url
>
jdbc:oracle:thin:@3.242.164.53:1521:ora
</
connection-url
>
<!--

Here are a couple of the possible OCI configurations.
For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly.
-->
<
driver-class
>
oracle.jdbc.driver.OracleDriver
</
driver-class
>
<
user-name
>
ming
</
user-name
>
<
password
>
tata123
</
password
>
<!--
Uses the pingDatabase method to check a connection is still valid before handing it out from the pool
-->
<!--
valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name
-->
<!--
Checks the Oracle error codes and messages for fatal errors
-->
<
exception-sorter-class-name
>
org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
</
exception-sorter-class-name
>
<!--
sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->

<!--
sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!--
corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional)
-->
<
metadata
>
<
type-mapping
>
Oracle9i
</
type-mapping
>
</
metadata
>
</
local-tx-datasource
>

</
datasources
>
jsp page ------------welcome.jsp
<%
@ page contentType
=
"
text/html; charset=gbk
"
language
=
"
java
"
%>
<%
@ page
import
=
"
javax.naming.Context
"
%>
<%
@ page
import
=
"
javax.sql.DataSource
"
%>
<%
@ page
import
=
"
javax.naming.InitialContext
"
%>
<%
@ page
import
=
"
java.sql.*
"
%>


<%
DataSource ds
=
null
;
Context initCtx
=
new
InitialContext();
//
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//
ds = (DataSource)envCtx.lookup("java:jdbc/BSTxDataSource");
ds
=
(DataSource)initCtx.lookup(
"
ming
"
);
if
(ds
!=
null
)
...
{
out.println("Connection is OK!");
Connection cn=ds.getConnection();
if(cn!=null)...{
out.println("cn is Ok!");
Statement stmt = cn.createStatement();
ResultSet rst = stmt.executeQuery("select * from log_users");
out.println("<p>rst is Ok!" + rst.next());
while(rst.next())...{
out.println("<P>username:" + rst.getString(2));
}
cn.close();
}else...{
out.println("rst Fail!");
}
}
else
out.println(
"
Fail!
"
);
%>
<
hr color
=
"
red
"
>
-----------------------------------------------
<%
DataSource ds2
=
null
;
Context initCox
=
new
InitialContext();
ds2
=
(DataSource)initCox.lookup(
"
mingtest
"
);
String sql
=
"
select U_NAME from log_users
"
;
if
(ds2
!=
null
)
...
{
out.print("ds2 isn't null!<br>");
Connection conn2 = ds2.getConnection();
Statement st = conn2.createStatement();
ResultSet rs2 = null ;
rs2 = st.executeQuery(sql);
while(rs2.next())
...{
String name = rs2.getString("U_name");
out.print("UserName:"+name+"<br>");
}
conn2.close();
}
else
out.println(
"
ds2 is null!
"
);
%>
本文介绍如何在JBoss应用服务器中配置Oracle数据源,包括oracle-ds.xml文件的具体设置,以及通过JNDI获取数据源的方法。此外,还提供了一个JSP页面示例,演示如何使用配置的数据源进行数据库操作。
1018

被折叠的 条评论
为什么被折叠?



