简体   繁体   English

将 sql 2008 R2 与 eclipse 中的 Java 连接

[英]Connect sql 2008 R2 with Java in eclipse

I am trying to connect the Sql Server 2008 R2 with java using JDBC.I have downloaded the jdbc jar files and i added in the eclipse.When i try to connect to sql 2008 R2 it says the following error.I am using the default port 1433. whether i have to change setting on the sql side. I am trying to connect the Sql Server 2008 R2 with java using JDBC.I have downloaded the jdbc jar files and i added in the eclipse.When i try to connect to sql 2008 R2 it says the following error.I am using the default port 1433. 我是否必须更改 sql 端的设置。

This is my code.这是我的代码。

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

the error which i am getting while connecting to sql is我在连接到 sql 时遇到的错误是

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host ENMEDIA-EA6278E, port 1433 has failed. com.microsoft.sqlserver.jdbc.SQLServerException:与主机 ENMEDIA-EA6278E、端口 1433 的 TCP/IP 连接失败。 Error: "Connection refused: connect. 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.".错误:“连接被拒绝:连接。验证连接属性,检查 SQL 服务器实例是否正在主机上运行并在端口接受 TCP/IP 连接,并且没有防火墙阻止 TCP 连接到端口。”。 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:577) at com.microsoft.sqlserver.Z84BEFFD at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login (SQLServerConnection.java:833) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:577) at com.microsoft. sqlserver.Z84BEFFD 3A0D49636A58CE6080CAA87C7Z.SQLServerDataSource.getConnection(SQLServerDataSource.java:57) at SocketClient.SocketClient.main(SocketClient.java:23) 3A0D49636A58CE6080CAA87C7Z.SQLServerDataSource.getConnection(SQLServerDataSource.java:57) 在 SocketClient.SocketClient.main(SocketClient.java:23)

Can anyboby point me where i went wrong.Thanks in advance.Any tutorial for connecting sql with java along with installation of任何人都可以指出我哪里出错了。在此先感谢。任何将 sql 与 java 连接以及安装的教程

Check if SQL Server is configured (via Surface Area Configuration) to accept remote TCP/IP connections.检查 SQL 服务器是否已配置(通过外围应用配置)以接受远程TCP/IP 连接。

Connection refused: connect.连接被拒绝:连接。 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.验证连接属性,检查 SQL 服务器实例是否正在主机上运行并在端口上接受 TCP/IP 连接,并且没有防火墙阻止 TCP 连接到该端口。

The hints in your stacktrace are pointing to a blocked connection.您的堆栈跟踪中的提示指向阻塞的连接。 Would be worthwile to check if you can get a connection through other means (ping, database client, telnet, ..).值得检查您是否可以通过其他方式(ping、数据库客户端、telnet、..)获得连接。 There is an Eclipse plugin to explore databases too;还有一个 Eclipse 插件来探索数据库; if you can get the connection working there..如果你能让连接在那里工作..

Cheers, Wim干杯,维姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM