gpt4 book ai didi

java - 在eclipse中连接sql 2008 R2与Java

转载 作者:行者123 更新时间:2023-12-02 08:14:48 25 4
gpt4 key购买 nike

我正在尝试使用 JDBC 将 Sql Server 2008 R2 与 java 连接。我已经下载了 jdbc jar 文件并添加到了 eclipse 中。当我尝试连接到 sql 2008 R2 时,它显示以下错误。我正在使用默认端口 1433。我是否必须更改 sql 端的设置。

这是我的代码。

package SocketClient;

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

public class SocketClient {

public static void main(String[] args) {

// Declare the JDBC objects.
Connection con = null;
CallableStatement cstmt = null;
ResultSet rs = null;

try {
// Establish the connection.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("sa");
ds.setPassword("password123");
ds.setServerName("ENMEDIA-EA6278E\\ENMEDIA");
ds.setPortNumber(1433);
ds.setDatabaseName("DishTV_Voting");
con = ds.getConnection();

// Execute a stored procedure that returns some data.
cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");
cstmt.setInt(1, 50);
rs = cstmt.executeQuery();

// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println("EMPLOYEE: " + rs.getString("LastName") +
", " + rs.getString("FirstName"));
System.out.println("MANAGER: " + rs.getString("ManagerLastName") +
", " + rs.getString("ManagerFirstName"));
System.out.println();
}
}

// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
System.exit(1);
}
}
}

我在连接到 sql 时遇到的错误是

com.microsoft.sqlserver.jdbc.SQLServerException:与主机 ENMEDIA-EA6278E、端口 1433 的 TCP/IP 连接失败。错误:“连接被拒绝:连接。请验证连接属性,检查 SQL Server 实例是否正在主机上运行并在端口上接受 TCP/IP 连接,并且没有防火墙阻止与该端口的 TCP 连接。”。 在 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170) 在 com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper (SQLServerConnection.java:1049) 在 com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833) 在 com.microsoft.sqlserver.jdbc.SQLServerConnection.connect (SQLServerConnection.java:716) 在 com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal (SQLServerDataSource.java:577) 在 com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57) 在 SocketClient.SocketClient.main(SocketClient.java:23)

任何人都可以指出我哪里出错了。提前谢谢。任何有关将 sql 与 java 连接以及安装的教程

最佳答案

检查 SQL Server 是否配置为(通过 Surface Area Configuration)接受远程 TCP/IP 连接。

关于java - 在eclipse中连接sql 2008 R2与Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6662577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com