SQLServer Express R2的JDBC连接URL [英] JDBC connection Url for SQLServer express R2

查看:332
本文介绍了SQLServer Express R2的JDBC连接URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下与sql server express r2连接的行:

I wrote the below line for connection with sql server express r2:

Connection con = DriverManager.getConnection( jdbc:sqlserver:// localhost / SQLEXPRESS; databaseName = abc, sa,密码);

Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost/SQLEXPRESS;databaseName=abc","sa","password");

正在给出例外:
com.microsoft.sqlserver.jdbc.SQLServerException:TCP /与主机localhost / SQLEXPRESS的IP连接,端口1433失败。错误:空。请验证连接属性,检查SQL Server实例是否在主机上运行并且在端口上接受TCP / IP连接,并且没有防火墙阻止与端口的TCP连接。

It is giving exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/SQLEXPRESS, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

我遵循以下步骤来解决异常:

I followed the line to solve the exception:

SQL Server Express的TCP / IP
默认情况下,TCP / IP for SQL Server Express已被禁用,因此JDBC无法连接到它,并且您可能会收到以下异常……

TCP/IP for SQL Server Express By default, TCP/IP for SQL Server Express is disabled, so JDBC cannot connect to it and you may get the following exception …

网络错误IOException:连接被拒绝:connect
启用TCP / IP

Network error IOException: Connection refused: connect Enable TCP/IP

要启用TCP / IP,请启动SQL Server配置管理器。

To enable TCP/IP, start SQL Server Configuration Manager.

扩展SQL Server 2005网络配置节点。
在右窗格中,选择SQLEXPRESS的协议。现在,右窗格应显示协议和状态列。
从TCP / IP上下文菜单中选择启用。
查找或配置TCP / IP端口

Expand SQL Server 2005 Network Configuration node. In the right pane, select Protocols for SQLEXPRESS. The right pane should now show Protocols and Status columns. Select Enable from the TCP/IP context menu. Find or Configure TCP/IP Port

启用TCP / IP后,必须找出要使用的端口号。 SQL Server Express每次启动时都会动态分配一个端口,因此要查找或配置端口号,请继续使用SQL Server Configuration Manager…

After enabling TCP/IP, you have to find out which port number to use. SQL Server Express allocates a port dynamically each time it is started, so to find or configure the port number, continue using SQL Server Configuration Manager …

从TCP / IP上下文菜单。 TCP / IP属性对话框应打开。
选择IP地址选项卡。
在IPAll节点中……
TCP动态端口字段显示当前使用的端口号。如果将该字段设置为空白,则SQL Server Express在重新启动时不应自动选择另一个端口。
在 TCP端口字段中设置所需的端口号。
按确定应用设置并关闭对话框。

Select Properties from the TCP/IP context menu. The TCP/IP Properties dialog should open. Select the IP Addresses tab. In the IPAll node … The TCP Dynamic Ports field shows the currently used port number. If you set that field to blank, then SQL Server Express should not automatically choose another port when it restarts. Set the desired port number in the TCP Port field. Press OK to apply your settings and close the dialog.

在此之后,连接也出现相同的异常。

After that also connection giving same exception.

推荐答案

尝试运行一些Java代码进行测试。将{computer-name} \SQLEXPRESS替换为您在SQL Server Management Studio的根节点中看到的域。安装SQL Express时,可以选择将 SQLEXPRESS的名称更改为自定义名称,因此可能也有所不同。要获取端口号,请转到Windows开始菜单> SQL Server 2008 R2>配置工具> SQL Server配置管理器>展开 SQL Server网络配置>单击 SQLEXPRESS的协议>右键单击 TCP / IP,然后选择属性。 >单击 IP地址选项卡/菜单>在Windows XP中,您将看到 IPAll ..查看那里的端口。它应默认为1433。如果您有2个版本的SQL Express,则可能有所不同。另外,请在Windows控制面板>管理工具>服务> SQL Server(SQLEXPRESS)中确保服务正在运行(状态为已启动)。 Windows 7稍有不同,但是您会发现它。

Try running some Java code to test it. Replace {computer-name}\SQLEXPRESS with the domain that you see in the root node of SQL Server Management Studio. When installing SQL Express, you had the option to change the name of "SQLEXPRESS" to something custom, so that might also be different. To get the port number, go to Windows Start Menu > SQL Server 2008 R2 > Configuration Tools > SQL Server Configuration Manager > expand "SQL Server Network Configuration" > Click "Protocols for SQLEXPRESS" > right click "TCP/IP" and choose Properties > click "IP Addresses" tab/menu > in Windows XP, you will see "IPAll" .. look at the port there. It should default to 1433. If you have 2 version of SQL Express, then it might be different. Also make sure your service is running (has status of "Started") in Windows Control Panel > Administrative Tools > Services > SQL Server (SQLEXPRESS). Windows 7 is slightly different, but you'll find it.

双反斜杠必须在Java字符串中。

The double backslashes need to be in a Java string. That just prints a single backslash during run time because it's an escape sequence.

import java.sql.*;

public class TestConnection
{
    public static void main(String[] args)
    {
        DB db = new DB();
        db.dbConnect("jdbc:sqlserver://{computer-name}\\SQLEXPRESS:1433;databaseName=abc;integratedSecurity=true;","sa","password");
    }
}

class DB
{
    public DB() {}

    public void dbConnect(String db_connect_string, String db_userid, String db_password)
    {
        try
        {
            Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
            System.out.println("connected");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};

这篇关于SQLServer Express R2的JDBC连接URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆