24

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?

Fowl
  • 4,940
  • 2
  • 26
  • 43
Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • Habe a look on the tutorial how to do it from IntelliJ-based IDEs https://blog.jetbrains.com/datagrip/2016/10/07/connecting-datagrip-to-sql-server-express-localdb/ – moscas Dec 07 '17 at 16:19

4 Answers4

37

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
bonh
  • 2,863
  • 2
  • 33
  • 37
  • 1
    These are great instructions, however I had to drop the database name from the connection string (since we're using the master database) and use my network credentials instead: jdbc:jtds:sqlserver://./;instance=LOCALDB#111111;namedPipe=true;domain=MY_DOMAIN – Ali Cheaito Sep 21 '15 at 13:57
  • 3
    And if we CREATE DATABASE SomeDB after connecting from sqlcmd, then we use: jdbc:jtds:sqlserver://./SomeDB;instance=LOCALDB#111111;namedPipe=true;domain=MY_DOMAIN – Ali Cheaito Sep 21 '15 at 13:58
  • 3
    ...finally, a JTDS 1.3.2 jar can be found in the dist zip on github: https://github.com/milesibastos/jTDS/releases – Ali Cheaito Sep 21 '15 at 15:07
  • 2
    What's changed since this answer and jTDS release 1.3.2, that today I cannot connect using this method? – ajeh May 30 '18 at 21:27
  • 1
    tried so many combinations I couldn't believe it then tried @AliCheaito suggestion with adding domain and it worked. Fantastic! Thanks – raddevus Oct 26 '21 at 20:39
22

Is it possible to connect to a SQL Server LocalDB using JDBC?

Not with Microsoft's JDBC Driver.

The jTDS JDBC driver supports named pipes.

Executing SqlLocalDB.exe info MyInstance will get you (along with other info) the instance pipe name such as "np:\.\pipe\LOCALDB#F365A78E\tsql\query".

Do you know of a workaround or change in status?

Possible workarounds are using alternative JDBC drivers or switching to SQL Server 2012 Express instead of LocalDB.

Details:

The Microsoft JDBC Driver is not compatible with LocalDB.

"Unfortunately, the Microsoft JDBC Driver does not support connecting to LocalDB. This happens because LocalDB only supports Named Pipes connections and our current JDBC implementation does not support Named Pipes. One possible work around for your developers is to download and install SQL Express, which and enable its TCP/IP support."

Luiz Fernando Santos (MSFT) July 06, 2012

"Unfortunately JDBC driver doesn't support LocalDB at this moment and there is no easy workaround. The team is aware of this missing feature, but filing a connect item is always helpful for DCR tracking and prioritization."

Krzysztof Kozielczyk - MSFT 22 Dec 2011

"Do you use SQL Server Express today for local development? Are you working on Windows or another platform? It would be great to hear more about how you would like to use LocalDB with your Java app."

Shamitha Reddy, Program Manager Microsoft JDBC Driver for SQL Server, Microsoft JDBC Driver Product Team 13 Apr 2012

Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
  • 2
    Thanks for taking the time to post. I had asked my DBA this very question. He in turn posed it on MSDN forum and got a reply from Luiz Fernando Santos. The same reply that you have kindly quoted! :-) – Sri Sankaran Mar 02 '13 at 19:54
  • Fair enough. I complemented the answer with possible workarounds. – Fernando Correia Mar 03 '13 at 17:16
0

I did my research today to setup connection using jTDS and named pipes.

The state at today is: IMPOSSIBLE! There is not possible to build connect string to localDB using jTDS due to its pipe name limitations. See open issue here: http://sourceforge.net/p/jtds/bugs/716/

As mentioned localDB does not supports other connection but namedPipes, so it looks like mission impossible so far...

rgds

Edit: As mentioned in comment there is patch in issue linked above and you can use this to fix the problem. Sorry I can not check this for myself.

Saram
  • 1,500
  • 1
  • 18
  • 35
  • I just had success with bonh's answer. So it's at least possible to get a connection. – Wisco crew Aug 19 '15 at 19:04
  • 2
    The bug that you linked to has a patch attached which purportedly fixes the limitation. – twm Dec 14 '15 at 19:18
  • People are using an unofficial fork of jTDS to workaround this bug. Sadly, the latest official version of jTDS is 1.6.1 (dated 2013), which means it is not being maintained. – Diego Queiroz Sep 04 '17 at 17:17
0

To Know LocalDb & Instance :

  1. Right click on LocalDb in Object explorer
  2. Select Properties
  3. click on view connection properties
  4. you will get all information about local and webserver details