SQL Server Is there any way to use JDBC to connect to local MS SQL without enabling TCP?

zkure5ic  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(102)

Background: My local MS SQL database is configured as following: TCP/IP disabled with dynamic port (default setting)

When using .Net to connect to my local database, I don't need to enable TCP connection in SQL Server Configuration Manager or specifying the port number in connection string. I just need to put this in app.config

<add name="MyDatabase" connectionString="Initial Catalog=MyDatabase;Data Source=localhost\SQLEXPRESS;Integrated Security=SSPI;"/>

However, I have to enable TCP in order to connect to it using JDBC. I wonder whether there is any way to get around it?

0aydgbwb

0aydgbwb1#

The Microsoft SQL Server JDBC driver is a Type 4 (pure Java) implementation, and it only supports TCP/IP sockets to communicate with SQL Server, therefor it requires TCP/IP to be enabled on the server instance. There is no workaround that magically allows you to connect otherwise.

As also indicated by the comment of vasily.sib, you can switch to jTDS which supports named pipes. See also Connecting to SQL Server LocalDB using JDBC . Just be aware that jTDS is - as far I'm aware - no longer maintained, so it doesn't support newer SQL Server versions (though it will connect) and newer JDBC specifications.

相关问题