Reputation: 2209
I have installed SQL sever 2008 locally It has many instance. How do I specify the instance name in jdbc string.
when I access the database using MS SQL studio server name looks like mymachinename\mssqlserver1
Upvotes: 1
Views: 27742
Reputation: 632
This is a full URL Example with a differente port and an instance name too
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
return DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORT;instanceName=INSTANCE;databaseName=DATABASE;", "sa", "PASSWORD);
Upvotes: 0
Reputation: 509
I had to first find the port of the instance. One can do this by firing the Query
SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = @@SPID
or by logging into the DB Server
And then used the connection string jdbc:sqlserver://db.mssql.company.com\InstanceName:56326;databaseName=DatabaseName;
Upvotes: 1
Reputation: 691725
Here's what you find when you google for "SQLServer JDBC URL": http://msdn.microsoft.com/en-us/library/ms378428%28v=sql.100%29.aspx
The general form of the connection URL is
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
where:
jdbc:sqlserver:// (Required) is known as the sub-protocol and is constant.
serverName (Optional) is the address of the server to connect to. This could be a DNS or IP address, or it could be localhost or 127.0.0.1 for the local computer. If not specified in the connection URL, the server name must be specified in the properties collection.
instanceName (Optional) is the instance to connect to on serverName. If not specified, a connection to the default instance is made.
portNumber (Optional) is the port to connect to on serverName. The default is 1433. If you are using the default, you do not have to specify the port, nor its preceding ':', in the URL.
Note:
For optimal connection performance, you should set the portNumber when you connect to a named instance. This will avoid a round trip to the server to determine the port number. If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.
Upvotes: 4