Datasource JNDI connectivity steps in java - Java @ Desk

Thursday, May 23, 2013

Datasource JNDI connectivity steps in java

JNDI Datasource connection in JBOSS

1) Create a dataSource-ds.xml in $jboss_home/server/default/deploy folder and edit it:
2)
<datasources>
<local-tx-datasource>
<jndi-name>MyDSJndi</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/DATASERVICE </connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>userName</user-name>
<password>password</password>
</local-tx-datasource>
</datasources>




3) Place JDBC driver jar in the $jboss_home/server/default/lib folder

4) Start the jboss server

Java code to get database connection:

Context initialContext = new InitialContext();
DataSource datasource = (DataSource)initialContext.lookup(“java: MyDSJndi”);
Connection con = ds.getConnection();







2 comments: