You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by John McAuley <jo...@dmc.dit.ie> on 2002/06/10 14:52:52 UTC
Urgent help -- Microsoft SQL SERVER connection
Hi folks,
I am trying to use Microsoft's jdbc driver for ms sql with Tomcat and am running into trouble with the JNDI lookup method. I look for a datasource and get null. If I use the class.forname method the driver is found and works perfectly but I am unable to get it working with the JNDI look up, This is very frustrating and seems to crop up a couple of times with fellow users. Any thoughts ??
Help much appreciated.
John
Server.xml
<Context path="/tools" docBase="tools" debug="0" reloadable="true">
<Resource name="jdbc/ToolsDB" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/ToolsDB">
<parameter>
<name>user</name>
<value>user</value>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:microsoft:sqlserver://IP-ADDRESS-of-server:1433</value>
</parameter>
</ResourceParams>
</Context>
web.xml
<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>
jdbc/ToolsDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
Code Snippet
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
out.println("Got first context.");
NamingEnumeration enum =ctx.listBindings("java:comp/env/jdbc");
while( enum.hasMore() ) {
out.println("Binding: " + ((Binding)enum.next()).toString() + "<br>");
}
DataSource ds = (DataSource)envCtx.lookup("jdbc/ToolsDB");
out.println("Got the second context." + "<br>" + "<br>");
if (ds != null)
{
out.println("Getting the connection from the context.");
Connection connection = ds.getConnection();
}else{
out.println("not working");
}