Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server LocalDB using JDBC

Tags:

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?

like image 310
Sri Sankaran Avatar asked Jul 05 '12 13:07

Sri Sankaran


2 Answers

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
like image 100
bonh Avatar answered Sep 27 '22 19:09

bonh


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

like image 29
Fernando Correia Avatar answered Sep 27 '22 19:09

Fernando Correia