JBoss DataSource Configuration and Test

JBoss DataSource Configuration and Test

This demo will show you how to do JBoss DataSource Configuration and Test it on running JBoss server.

Here we will configure MSSQL database with JBoss server test our connection using JSP file. Please follow below steps:

  • Copy jtds-1.2.jar to server\default\lib folder
  • Copy below mssql-ds.xml file to server\default\deploy directory:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
  <jndi-name>DefaultDS</jndi-name>
  <connection-url>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test</connection-url>
  <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
  <user-name>userid</user-name>
  <password>password</password>
  <check-valid-connection-sql>SELECT 1 FROM sysobjects</check-valid-connection-sql>
  <metadata>
	  <type-mapping>MS SQLSERVER2000</type-mapping>
  </metadata>
  </local-tx-datasource>
</datasources>
  • As you see above database name is “test” using default with MS SQL Server 2000.
  • Please don’t forget to change URL and username/password based on your environment.

 

Test using sample JDBC client:

  • Please create folder name: “jdbcclient.war” inside \server\default\deploy directory and create below jsp file and keep inside “jdbcclient.war” folder:
<%@page contentType="text/html"
 import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"
 %>
 <%
 	DataSource ds = null;
 	Connection con = null;
 	PreparedStatement pr = null;
 	InitialContext ic;
 	try {
 		ic = new InitialContext();
 		ds = (DataSource) ic.lookup("java:/DefaultDS");
 		con = ds.getConnection();
 		pr = con.prepareStatement("SELECT userId FROM UTDB..DOUserTable");
 		ResultSet rs = pr.executeQuery();
 		while (rs.next()) {
 			out.println("<br> " + rs.getString("userId"));
 		}
 		rs.close();
 		pr.close();
 	} catch (Exception e) {
 		out.println("Exception thrown " + e);
 	} finally {
 		if (con != null) {
 			con.close();
 		}
 	}
 %>

 

  • Now start your JBoss server (If you are using window operating system then go to jboss-xxx\bin folder and double click run.bat file to start the server:
  • Cut and paste this URL in your browser: http://localhost:8080/jdbcclient/client.jsp where you see list of users in the user table:

JBoss DataSource Configuration and Test

For more information please visit this JBoss tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *