Connecting to SQL Server from Java over TCP/IP

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.

About these ads

Posted on July 7, 2012, in Uncategorized and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 263 other followers