trouble connecting with Microsoft jdbc driver

Mark used Ask the Experts™
I have a strange problem. I've set up a new tomcat installation on a new Linux Slackware computer. I am doing the following:

try {
  con = DriverManager.getConnection(jdbc:sqlserver://sqlhost\\instance:1433;loginTimeout=10;user=myuser;password=mypass;");
catch (SQLExceltion e) { .... }

This works just fine in serveral java programs I have written on this machine, but when I use this in jsp I get an exception. e.getErrorCode() is zero; e.getMessage() is ""cantleys: cantleys: Name or service not known"

I am at a loss about this error. I have successfully installed java and tomcat on three other Linux hosts w/o this problem. "cantleys" is the hostname of this Linux computer. Why is it mentioning it as a service name? Why does this only happen with jsp but not regular java?

I am using the same tomcat version and same sqljdbc.jar on all machines. I've put the sqljdbc.jar file in .../webapps/myapp/WEB-INF/lib and in $CATALINA_HOME/lib, and it both places at once, no difference. If I remove the sqljdbc.jar file from these libs I get java.lang.ClassNotFoundException:, so I know it is using the correct jarfile.
This is killing me! I've been messing with it for hours and comparing with the other computers. Help! I've got a deadline!!!

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I would suggest to check the following:

1. check the port 1433 is free (means not used by any other process) on canleys. If this is the case, stop that process make the port free and try again. Same way check for other linux boxes

2. check and compare "/etc/hosts" file on each box you have installed the app. If found different and update the "/etc/hosts" file on canleys accordingly.
Top Expert 2016

Open in new window

I'm assuming those are obscured versions of what you really have there. Check that you can ping 'sqlhost'. If you can't, it might be a name resolution problem, in which case you need to fix that, or use the IP address instead of the name


amit_n_panchal: yes, the etc/hosts files are all different, but are all at different locations. None are in the same domain.

CEHJ: "I'm assuming those are obscured versions of what you really have there"

Yes, obscured. "sqlhost", "instance", "myuser", and "mypass" are fake, but otherwise the connection URL is exactly as I've shown.

Here's the thing that I must not have made clear enough. On this very same host I can write java programs that connect to the database just fine. The sqljdbc.jar in the CLASSPATH is the exact same jarfile used by tomcat. The connection URL used by the java program is exactly the same one used by the jsp programs (I printed them both out). Port 1433 work fine for the java programs. Plus, I know from experience that there is a different message if the right port is not opened.

The only think I can think of that is different is that tomcat is "running" the jsp programs; or is somehow involved (or in this case interferring). Can you think of a config setting that might be causing a problem? Does it need some additional port on the SQL Server host?

I just set this whole thing up on another Linux machine a few weeks ago and I've essentially copied the tomcat hierarchy to the new machine. The difference between the machines are the local hostname, the target SQL Server and the connection URL. The problem's gotta be somewhere in that nexus (but don't let me stifle imagination!)

Like I said, plain 'ole java programs work fine connecting (a command path issue?)

The ONLY difference
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.


more info: I've redirected the connection URL to point to a friend's SQL Server host across town, and visa versa: redirected his connection URL to point to my SQL Server host.

I can query from the friend's workstation to my database both at the java/command-line level AND via jsp.

I can query from my computer to my friend's SQL Server at the java/command-line level, but NOT via jsp. Yet he, of course, can query from his own computer to his own SQL Server with BOTH java/command-line and jsp.

I think this eliminates my SQL Server as being the problem.

My computer is a more-or-less clone of his for setup of java, tomcat, apache, etc., etc. (in fact, I set up his computer too!) HOWEVER, there must be something different with my config that prevents tomcat/jsp from connecting to either my SQL Server or his SQL server.

Any ideas where to look? Meanwhile, I will embark on a file-by-file, line-by-line comparison of all things tomcat.
HEY!!!! I figured it out. I needed to have the local hostname in the /etc/hosts file. I'm not sure why this didn't get put in there when the system was built; perhaps I didn't know what to call it at the time. I found only 1 post on google with the string "Name or service not found" and it had to do with someone trying to get telnet working. telnet wouldn't work unless the localhost was set in /etc/hosts. I thought, "what the heck, let's see", and sure enough, localhost was set in my friend's computer, but not in mine. When I set it, it worked!!! Man, that was a painful couple of days.
Top Expert 2016

:) Glad you fixed it


points for the effort.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial