JDBC连接失败,错误:与主机的TCP / IP连接失败 [英] JDBC connection failed, error: TCP/IP connection to host failed
本文介绍了JDBC连接失败,错误:与主机的TCP / IP连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将Java类文件与SQL Server 2012连接。我已经使用SQL Server身份验证登录。但我收到连接错误。
I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication. But i am receiving error in connectivity.
错误:
与主机127.0.0.1,端口1433的TCP / IP连接失败。错误:连接被拒绝:连接。验证连接属性。确保在主机上运行SQL Server实例并接受端口上的TCP / IP连接。确保防火墙不阻止与端口的TCP连接。
我的代码---
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}
推荐答案
- 打开SQL Server配置管理器,然后展开SQL Server 2012网络配置。
- 单击InstanceName的协议,然后确保在右侧面板中启用TCP / IP并双击TCP / IP。
- 在协议选项卡上,注意收听所有项的值。
- 单击IP地址选项卡:
如果Listen All的值为yes,此SQL Server 2012实例的TCP / IP端口号是IPAll下TCP动态端口项的值。
如果Listen All的值为no,则此SQL Server 2012实例的TCP / IP端口号是特定IP地址的TCP动态端口项的值。 - 确保TCP端口为1433.
- 单击确定。
- Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
- Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
- On the Protocol tab, notice the value of the Listen All item.
- Click the IP Addresses tab: If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll. If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
- Make sure the TCP Port is 1433.
- Click OK.
如果有如有任何问题,请告诉我。
If there are any more questions, please let me know.
谢谢。
这篇关于JDBC连接失败,错误:与主机的TCP / IP连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文