I am trying to connect my Microsoft sql server using JNDI method. My code is running as container. The details are given below
Below is my context.xml under META-INF
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/CIBILDB"
auth="Container"
type="javax.sql.DataSource"
validationQuery="SELECT 1"
validationInterval="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
jmxEnabled="true"
username="automationrobot"
password="Soft2007"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://TGSLAP-2154\\SQLEXPRESS:1433"/>
</Context>
and below is my java code
package com.CIBIL.dao;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class InitialiseCIBILDBConnection {
private static DataSource dataSource;
private static final String JNDI_LOOKUP_SERVICE = "java:/comp/env/jdbc/CIBILDB";
static{
try {
Context context = new InitialContext();
Object lookup = context.lookup(JNDI_LOOKUP_SERVICE);
if(lookup != null){
dataSource =(DataSource)lookup;
}else{
new RuntimeException("JNDI look up issue.");
}
} catch (NamingException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
When I use the above code, I am getting error
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Can some one please help me on how to resolve the issue?