com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/SQLEXPR

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.

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();
        }
    }
};

参照:http://stackoverflow.com/questions/9256044/jdbc-connection-url-for-sqlserver-express-r2

我把IPAll

tcp动态端口设成了空

tcp端口设成了1433

应为当时我是用的windows身份验证,我改成了sqlserver身份验证参照:http://msdn.microsoft.com/zh-cn/library/ms188670.aspx

最后写成

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		Connection connection = DriverManager.getConnection("jdbc:sqlserver://THINKPAD-PC\\SQLEXPRESS:1433;user=sa;password=xxx");

就连上了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值