Enterprise Integration Zone is brought to you in partnership with:

Dan Haywood is a UK-based freelance consultant specializing in enterprise application development using domain driven design approaches and agile development. Technology skills include Java, JEE, .NET, Sybase, RDBMS, OOAD, UML, AOP, TDD, Subversion and TFS Dan is a committer and advocate of the Naked Objects framework, and the author of “Domain Driven Design using Naked Objects”, published by the Pragmatic Bookshelf. Dan is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Connecting to SQL Server from Java over TCP/IP

07.17.2012
| 6411 views |
  • submit to reddit

Been a while since I did this; turns out it’s easy enough.

First, you’ll need the current JDBC driver from Microsoft, which can be found here.  Add to classpath as usual.

Set up your Java application to use the following JDBC connection settings:

  • ConnectionDriverName=com.microsoft.sqlserver.jdbc.SQLServerDriver
  • ConnectionURL=jdbc:sqlserver://127.0.0.1:1433;instance=SQLEXPRESS;databaseName=jdo;
  • ConnectionUserName=jdo
  • ConnectionPassword=jdopass

In this case I’m connecting to SQLExpress instance running on my localhost.

The trickiest bit (for me) was figuring out how to persuade SQL Server to listen over TCP/IP – by default it only seems to listen over shared memory.  Anyway, after a bit of trial and error, the following worked.

First, locate SQL Server Configuration Manager, and enable TCP/IP for the SQL Server instance (SQLExpress in this case).

image

Then, right-click to open up the properties for TCP/IP, and set the port for IPAll at the bottom:

image

You should then be able to connect.

Published at DZone with permission of Dan Haywood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mark Unknown replied on Wed, 2012/07/18 - 12:32pm

Turning on TCP/IP is the step i always seem to forget. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.