Jboss+oracle 的JNDI的配置
1,在jboss中修改oracle-ds.xml的配置文件
打开jboss安装目录,找到jboss-4.0.1sp1\docs\examples\jca下的oracle-ds.xml将其拷贝到jboss-4.0.1sp1\server\default\deploy下,并做相应修改,主要内容如下:
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS </jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:@10.25.10.175:1521:orcl</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>SCOTT</user-name>
<password>TIGER</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>
2,在java类中连接数据库,参考代码如下:
public static Connection getConnection() {
DataSource ds = null;
Context initCtx = null;
Connection conn = null;
try {
initCtx = new InitialContext();
//在这里对应的值是oracle-ds.xml中的<jndi-name>,要注意的是前面要加”java:”
ds = (DataSource) initCtx.lookup("java:OracleDS");
if (ds != null) {
System.out.println("JNDI连接成功!");
conn = ds.getConnection();
} else {
System.out.println("JNDI连接失败!");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("oracle connection parametre error!!!");
}
return conn;
}