Connecting to MS SQL Server 2012 Express through JDBC failed

In order to test a particular feature of our JEE application I installed a free copy of MS SQL Server 2012 Express. I’m a total MS SQL illiterate. As always with MS SQL I used the jTDS JDBC driver to connect from Java.

The first hurdle was to learn that MS SQL Express by default uses dynamic ports. To connect in a TCP/IP fashion from Java you need to configure static ports manually.

This allowed me to connect from Oracle SQL Developer (using the jTDS driver) but connecting from my Java application failed: “Cannot open database “<my-db-name>” requested by the login. The login failed”. This felt really odd. I double-checked the JDBC connection string and was confident everything was in good order (examples).

I experimented for a few hours until I found the culprit: the MS SQL database contained a ‘-‘ in its name. So, this had to go. Why this failed in the app but not with Oracle SQL Developer I don’t know. Didn’t bother analyzing once the connection could be established, sorry…

Leave a Reply