0

I'm trying to connect to a Microsoft SQL server via java, and I have no guarantees as to what the port number will be.

There are methods that I have found for getting the port number, but they all seem to not work or require signifigant user interraction: How to find the port for MS SQL Server 2008?

(The netstat method only works if a session is already open to the SQL server, which is not guaranteed)

The simplest one perhaps would be the registry, but there are many places in the registry to look based on the "instance name" and if there is a simpler way I would love to know it.

Community
  • 1
  • 1
Jdban101
  • 367
  • 1
  • 3
  • 21
  • Is the server running on your local machine? If not the only way is to just keep trying ports until you get it. – Cruncher Sep 10 '13 at 20:22
  • Yes, the server is running on the local machine. This is going to be installed to many machines though which can all have different ports. – Jdban101 Sep 10 '13 at 20:24
  • This sounds like you need to rethink design. What do you want to install a local sql server for so many machines for? – Cruncher Sep 10 '13 at 20:38
  • I'm not installing the SQL server, its already on the machines and I need to access it. – Jdban101 Sep 10 '13 at 20:40
  • Why do they all have different ports? A little bit of information into what the program does, might give context or an alternative – Cruncher Sep 10 '13 at 20:41
  • TCP/IP isn't required to connect to a local database. `select * from sys.dm_exec_connections as EC inner join sys.dm_exec_sessions as ES on ES.session_id = EC.session_id where EC.session_id = @@SPID` will give you the information for an established connection. – HABO Sep 10 '13 at 20:45
  • @Cruncher They all can have different ports because they were installed by other companies and I can't guarantee their installation conventions. – Jdban101 Sep 10 '13 at 20:49
  • @HABO I can't run the select command because I don't have a session with the databse. – Jdban101 Sep 10 '13 at 20:49
  • @Jdban101 If the software is getting licensed out to other companies, give them a config file where they can enter the port. Surely they know their ports. – Cruncher Sep 10 '13 at 20:52
  • How would you determine which instance to connect to if there are several running? Is there a guarantee that TCP/IP is an enabled protocol for the instance you want? – HABO Sep 10 '13 at 21:01
  • @Cruncher, The main one I can't do that is that I need to support already existing configurations with unknown port numbers. – Jdban101 Sep 10 '13 at 21:05
  • @HABO It would be fine because the data I am querying for would only be in one of them. There should be a guarantee that the TCP/IP is enabled. But if it wasn't, how would I connect? – Jdban101 Sep 10 '13 at 21:06
  • And there's the question: How _do_ you connect to a local instance that someone else (mis)configured? I usually use the Shared Memory protocol to connect to local instances. If I don't expect anyone outside the system to connect then I disable TCP/IP as a protocol for the instance(s) of SQL Server. – HABO Sep 10 '13 at 22:12
  • @HABO Is shared memory protocol possible in Java? I'm having some trouble googling around for it. I'm using JDBC currently. – Jdban101 Sep 11 '13 at 21:40
  • I would think that if you supply a connection string then you can use your choice of [protocol](http://www.connectionstrings.com/define-sql-server-network-protocol/). – HABO Sep 11 '13 at 23:32

0 Answers0