OC4J里配置数据源:
1.application.xml的<data-sources>元素必须指出data-sources.xml文件的文件名和路径,即:
<data-sources
path = "data-sources.xml"
/>
一般application.xml文件和data-sources.xml 文件都在j2ee/home/config/下。所以<data-sources>元素的path属性只包括data-sources.xml的文件名;
2.data-sources.xml文件中定义数据源:
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="OracleDS"
location="jdbc/OracleCoreDS"
xa-location="jdbc/xa/OracleXADS"
ejb-location="jdbc/OracleDS"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="hr"
password="hr"
url="jdbc:oracle:thin:@myhost:1521:ORCL"
inactivity-timeout="30"
/>
class属性定义需要定义的数据源类型;
location、xa-location和ejb-location定义JNDI名字;
connection-driver指出连接驱动。
例如:
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="MyConnection"
location="jdbc/MyConnection"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="portal"
password="pportalgis"
url="jdbc:oracle:thin:@172.18.2.6:1521:apps"
inactivity-timeout="30"
max-connections="50"
max-connect-attempts="2"
min-connections="4"
/>
2.程序调用:
Context env= new InitialContext();
DataSource pool= (DataSource)env.lookup("jdbc/MyConnection");
Connection conn=pool.getConnection();
oracle.jdbc.OracleConnection 分为:
getConnection(); //数据源中已定义用户名和密码
getConnection(String username, String password); //数据源中未定义用户名和密码
可以如下:
oracle.jdbc.OracleConnection conn =(oracle.jdbc.OracleConnection) pool.getConnection();