簡體   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. 我是否必須更改 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 服務器實例是否正在主機上運行並在端口接受 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 3A0D49636A58CE6080CAA87C7Z.SQLServerDataSource.getConnection(SQLServerDataSource.java:57) 在 SocketClient.SocketClient.main(SocketClient.java:23)

任何人都可以指出我哪里出錯了。在此先感謝。任何將 sql 與 java 連接以及安裝的教程

檢查 SQL 服務器是否已配置(通過外圍應用配置)以接受遠程TCP/IP 連接。

連接被拒絕:連接。 驗證連接屬性,檢查 SQL 服務器實例是否正在主機上運行並在端口上接受 TCP/IP 連接,並且沒有防火牆阻止 TCP 連接到該端口。

您的堆棧跟蹤中的提示指向阻塞的連接。 值得檢查您是否可以通過其他方式(ping、數據庫客戶端、telnet、..)獲得連接。 還有一個 Eclipse 插件來探索數據庫; 如果你能讓連接在那里工作..

干杯,維姆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM