Connecting to SQL Server LocalDB using JDBC
Asked Answered
S

4

27

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?

Scrutinize answered 5/7, 2012 at 13:49 Comment(1)
Habe a look on the tutorial how to do it from IntelliJ-based IDEs blog.jetbrains.com/datagrip/2016/10/07/…Emersonemery
T
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

Trivalent answered 2/3, 2013 at 15:23 Comment(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! :-)Scrutinize
Fair enough. I complemented the answer with possible workarounds.Trivalent
C
39

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
Congresswoman answered 25/6, 2014 at 14:53 Comment(5)
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_DOMAINRiproaring
And if we CREATE DATABASE SomeDB after connecting from sqlcmd, then we use: jdbc:jtds:sqlserver://./SomeDB;instance=LOCALDB#111111;namedPipe=true;domain=MY_DOMAINRiproaring
...finally, a JTDS 1.3.2 jar can be found in the dist zip on github: github.com/milesibastos/jTDS/releasesRiproaring
What's changed since this answer and jTDS release 1.3.2, that today I cannot connect using this method?Rolanda
tried so many combinations I couldn't believe it then tried @AliCheaito suggestion with adding domain and it worked. Fantastic! Thanks 👍🏽Oogonium
T
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

Trivalent answered 2/3, 2013 at 15:23 Comment(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! :-)Scrutinize
Fair enough. I complemented the answer with possible workarounds.Trivalent
B
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.

Brest answered 2/7, 2014 at 12:26 Comment(3)
I just had success with bonh's answer. So it's at least possible to get a connection.Bothnia
The bug that you linked to has a patch attached which purportedly fixes the limitation.Undersea
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.Pluperfect
B
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
Break answered 8/9, 2018 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.