datasource jboss Archive

Creating a datasource on JBoss

Place the oracle database driver (ojdbc14.jar) in C:\JBoss\jboss-5.1.0.CR1\server\default\lib

Create the datasource xml file and place it in C:\JBoss\jboss-5.1.0.CR1\server\default\deploy

oracle-ds.xml

<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi-name>
<connection-url>jdbc:oracle:thin:@192.168.96.95: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>
<check-valid-connection-sql>select * from dual</check-valid-connection-sql>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

Start the server

run -c default

Deploy the application that uses a datasrouce

index.jsp

<%@ page import=”java.sql.Connection”%>
<%@ page import=”java.sql.ResultSet”%>
<%@ page import=”java.sql.SQLException”%>
<%@ page import=”java.sql.Statement”%>
<%@ page import=”java.util.Properties”%>

<%@ page import=”javax.naming.Context”%>
<%@ page import=”javax.naming.InitialContext”%>
<%@ page import=”javax.naming.NamingException”%>
<%@ page import=”javax.sql.DataSource”%>

<%

Context ctx = null;
try {

Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,”org.jnp.interfaces.NamingContextFactory”);
prop.setProperty(Context.SECURITY_PRINCIPAL,”admin”);
prop.setProperty(Context.SECURITY_PRINCIPAL,”SECURITY_CREDENTIAL”);
prop.setProperty(Context.PROVIDER_URL, “jnp://localhost:1099”);
ctx = new InitialContext(prop);
DataSource ds = (DataSource) ctx.lookup(“java:OracleDS”);
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(“select * from dual”);
rset.close();
stmt.close();
conn.close();

}catch (Exception e) {
System.err.println(e.getMessage());
}

%>

Test the application!