java sqlserver连接字符串_生成连接 URL - SQL Server | Microsoft Docs

创建连接 URLBuilding the Connection URL

01/29/2020

本文内容

连接 URL 的一般形式为:The general form of the connection URL is

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

其中:where:

jdbc:sqlserver://(必需)称为子协议,且为常数 。jdbc:sqlserver:// (Required) is known as the subprotocol and is constant.

serverName(可选)是要连接到的服务器的地址 。serverName (Optional) is the address of the server to connect to. 它可以是 DNS 或 IP 地址,也可以是本地计算机地址 localhost 或 127.0.0.1。This could be a DNS or IP address, or it could be localhost or 127.0.0.1 for the local computer. 如果未在连接 URL 中指定服务器名称,则必须在属性集中指定。If not specified in the connection URL, the server name must be specified in the properties collection.

instanceName(可选)是 serverName 上要连接到的实例 。instanceName (Optional) is the instance to connect to on serverName. 如果未指定,则会连接到默认实例。If not specified, a connection to the default instance is made.

portNumber(可选)是 serverName 上要连接到的端口 。portNumber (Optional) is the port to connect to on serverName. 默认值为 1433。The default is 1433. 如果使用默认端口,则无需在 URL 中指定端口及其前面的“:”。If you're using the default, you don't have to specify the port, nor its preceding ':', in the URL.

备注

若要获得最佳连接性能,应在连接到指定实例时设置 portNumber。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. 如果同时使用 portNumber 和 instanceName,则会优先使用 portNumber,而忽略 instanceName。If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.

property(可选)是一个或多个选项连接属性 。property (Optional) is one or more option connection properties. 有关详细信息,请参阅设置连接属性。可指定该列表中的任何属性。Any property from the list can be specified. 属性只能用分号(“;”)分隔,且不允许重复。Properties can only be delimited by using the semicolon (';'), and they can't be duplicated.

注意

出于安全考虑,应避免根据用户输入的内容创建连接 URL。For security purposes, you should avoid building the connection URLs based on user input. 只应在 URL 中指定服务器名称和驱动程序。You should only specify the server name and driver in the URL. 对于用户名和密码值,请使用连接属性集。For user name and password values, use the connection property collections. 有关 JDBC 应用程序安全性的详细信息,请参阅保护 JDBC 驱动程序应用程序。For more information about security in your JDBC applications, see Securing JDBC driver applications.

连接示例Connection examples

使用用户名和密码连接到本地计算机上的默认数据库:Connect to the default database on the local computer by using a user name and password:

jdbc:sqlserver://localhost;user=MyUserName;password=*****;

备注

尽管前面的示例在连接字符串中使用了用户名和密码,但您应使用集成安全性,因为这样做更安全。Although the previous example uses a username and password in the connection string, you should use integrated security as it is more secure. 有关详细信息,请参阅本主题后面部分的通过集成身份验证进行连接一节。For more information, see the Connecting with Integrated Authentication section later in this topic.

以下连接字符串演示了如何使用集成身份验证和 Kerberos,从一个在 Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server 支持的任何操作系统上运行的应用程序连接到 SQL ServerSQL Server 数据库:The following connection string shows an example of how to connect to a SQL ServerSQL Server database using integrated authentication and Kerberos from an application running on any operating system supported by the Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server:

jdbc:sqlserver://;servername=server_name;integratedSecurity=true;authenticationScheme=JavaKerberos

使用集成身份验证连接到本地计算机上的默认数据库:Connect to the default database on the local computer by using integrated authentication:

jdbc:sqlserver://localhost;integratedSecurity=true;

连接到远程服务器上的指定数据库:Connect to a named database on a remote server:

jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;

连接到远程服务器上的默认端口:Connect on the default port to the remote server:

jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;integratedSecurity=true;

通过指定自定义应用程序名称进行连接:Connect by specifying a customized application name:

jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;applicationName=MyApp;

命名的多个 SQL Server 实例Named and multiple SQL Server instances

SQL ServerSQL Server 允许在每台服务器上安装多个数据库实例。allows for the installation of multiple database instances per server. 每个实例都由一个专用名称所标识。Each instance is identified by a specific name. 若要连接到指定的 SQL ServerSQL Server 实例,可以使用指定实例的端口号(首选),也可将实例名指定为 JDBC URL 属性或 datasource 属性 。To connect to a named instance of SQL ServerSQL Server, you can either specify the port number of the named instance (preferred), or you can specify the instance name as a JDBC URL property or a datasource property. 如果未指定实例名属性或端口号属性,则会创建与默认实例的连接。If no instance name or port number property is specified, a connection to the default instance is created. 请看以下示例:See the following examples:

若要使用端口号,请执行下列操作:To use a port number, do the following:

jdbc:sqlserver://localhost:1433;integratedSecurity=true;;

若要使用 JDBC URL 属性,请执行下列操作:To use a JDBC URL property, do the following:

jdbc:sqlserver://localhost;instanceName=instance1;integratedSecurity=true;;

转义连接 URL 中的值Escaping values in the connection URL

由于包含特殊字符(如空格、分号和引号),所以必须转义连接 URL 值的某些部分。You might have to escape certain parts of the connection URL values because of the inclusion of special characters such as spaces, semicolons, and quotation marks. 如果这些字符包含在大括号中,则 JDBC 驱动程序将支持对其进行转义。The JDBC driver supports escaping these characters if they are enclosed in braces. 例如,{;} 将转义分号。For example, {;} escapes a semicolon.

转义的值可以包含特殊字符(特别是“=”、“;”、“[]”和空格),但不能包含大括号。Escaped values can contain special characters (especially '=', ';', '[]', and space) but cannot contain braces. 应将必须进行转义且包含大括号的值添加到属性集中。Values that must be escaped and contain braces should be added to a properties collection.

备注

大括号内的空白为原义字符,不能删除。White space inside the braces is literal and not trimmed.

在 Windows 上通过集成身份验证进行连接Connecting with integrated authentication On Windows

JDBC 驱动程序支持通过 integratedSecurity 连接字符串属性在 Windows 操作系统上使用“类型 2”集成身份验证。The JDBC driver supports the use of Type 2 integrated authentication on Windows operating systems through the integratedSecurity connection string property. 若要使用集成身份验证,请将 mssql-jdbc_auth--.dll 文件复制计算机中 Windows 系统路径下的 JDBC 驱动程序安装目录中。To use integrated authentication, copy the mssql-jdbc_auth--.dll file to a directory on the Windows system path on the computer where the JDBC driver is installed.

mssql-jdbc_auth--.dll 文件的安装位置如下:The mssql-jdbc_auth--.dll files are installed in the following location:

\sqljdbc_\\auth\sqljdbc_\\auth\

有关 Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server 支持的任何操作系统,请参阅使用 Kerberos 集成身份验证连接到 SQL Server 以了解 {1}Microsoft JDBC Driver 4.0 for SQL Server{2}Microsoft JDBC Driver 4.0 for SQL Server 中新增的一个功能,该功能允许应用程序使用集成身份验证和类型 4 Kerberos 来连接数据库。For any operating system supported by the Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server, see Using Kerberos Integrated Authentication to Connect to SQL Server for a description of a feature added in {1}Microsoft JDBC Driver 4.0 for SQL Server{2}Microsoft JDBC Driver 4.0 for SQL Server that allows an application to connect to a database using integrated authentication with Type 4 Kerberos.

备注

如果运行的是 32 位的 Java 虚拟机 (JVM),则使用 x86 文件夹中的 mssql-jdbc_auth--.dll 文件,即使操作系统是 x64 版本也不例外。If you are running a 32-bit Java Virtual Machine (JVM), use the mssql-jdbc_auth--.dll file in the x86 folder, even if the operating system is the x64 version. 如果在 x64 处理器上运行 64 位 JVM,则使用 x64 文件夹中的 mssql-jdbc_auth--.dll 文件。If you are running a 64-bit JVM on a x64 processor, use the mssql-jdbc_auth--.dll file in the x64 folder.

也可以设置 java.library.path 系统属性来指定 mssql-jdbc_auth--.dll 的目录。Alternatively you can set the java.library.path system property to specify the directory of the mssql-jdbc_auth--.dll. 例如,如果 JDBC 驱动程序安装在默认目录中,您可以在 Java 应用程序启动时使用以下虚拟机 (VM) 参数来指定 DLL 的位置:For example, if the JDBC driver is installed in the default directory, you can specify the location of the DLL by using the following virtual machine (VM) argument when the Java application is started:

-Djava.library.path=C:\Microsoft JDBC Driver 6.4 for SQL Server\sqljdbc_\enu\auth\x86

通过 IPv6 地址进行连接Connecting with IPv6 addresses

JDBC 驱动程序支持结合使用 IPv6 地址以及连接属性集合和 serverName 连接字符串属性。The JDBC driver supports the use of IPv6 addresses with the connection properties collection, and with the serverName connection string property. 连接字符串不支持在 IPv6 地址中使用初始 serverName 值,如 jdbc:sqlserver://serverName 。The initial serverName value, such as jdbc:sqlserver://serverName, isn't supported for IPv6 addresses in connection strings. 使用 serverName 的名称而不是原始 IPv6 地址将适用于连接中的所有情况 。Using a name for serverName instead of a raw IPv6 address will work in every case in the connection. 以下实例提供了详细信息。The following examples provide more information.

使用 serverName 属性To use the serverName property

jdbc:sqlserver://;serverName=3ffe:8311:eeee:f70f:0:5eae:10.203.31.9\\instance1;integratedSecurity=true;

使用属性集合To use the properties collection

Properties pro = new Properties();

pro.setProperty("serverName", "serverName=3ffe:8311:eeee:f70f:0:5eae:10.203.31.9\\instance1");

Connection con = DriverManager.getConnection("jdbc:sqlserver://;integratedSecurity=true;", pro);

另请参阅See also

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值