Connecting to SQL Server LocalDB using JDBC

Sri Sankaran picture Sri Sankaran · Jul 5, 2012 · Viewed 20.3k times · Source

Is it possible to connect to a SQL Server LocalDB using JDBC? It appears that (as of Dec 2011) it was not possible.

Do you know of a workaround or change in status?

Answer

bonh picture bonh · Jun 25, 2014

Yes, it is possible.

The connection string for a LocalDB instance using jTDS looks like this:

jdbc:jtds:sqlserver://./DatabaseName;instance=LOCALDB#88893A09;namedPipe=true

This works as of jTDS 1.3.2. You can download a release here:

https://github.com/milesibastos/jTDS/releases/download/v1.3.2/jtds-1.3.2-dist.zip

To find the named pipe for your desired LocalDB, run

SqlLocalDb info NameOfTheLocalDBInstance

which will give you something like np:\\.\pipe\LOCALDB#88893A09\tsql\query

It's probably best to connect with a specific username/password, so create a login and user for your database in that LocalDB instance as well (if you haven't already):

sqlcmd -S np:\\.\pipe\LOCALDB#88893A09\tsql\query

CREATE LOGIN dbuser WITH PASSWORD = 'dbpassword'
GO
CREATE USER dbuser
GO
ALTER AUTHORIZATION ON DATABASE::DatabaseName TO dbuser
GO