2014-02-17 123 views
1

好了,所以我有这样的代码:的Java连接到SQL Server

package com.andrewxd.banksystem; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

public class Interface 
    { 
     public static void main(String[] args) 
     { 
      try 
      { 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
       Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem"); 
       System.out.println("test"); 
       Statement sta = conn.createStatement(); 
       String Sql = "select * from Clients"; 
       ResultSet rs = sta.executeQuery(Sql); 

       System.out.println(rs.next()); 
      } catch (Exception e){ 
       e.printStackTrace(); 
      } 
    } 


} 

,但它给了我这个错误可以有人帮助我?:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". 

我试图寻找,但我没有”真的很了解。

从我的理解端口是不正确的,但我如何找到正确的IP /端口?

+0

从我的理解端口是不正确的,但我如何找到正确的IP /端口? –

+0

如果您有多个网络接口,请尝试使用这些IP地址。 – RaviH

+0

这应该是在你的问题,否则它可以被错过。 – Batty

回答

1

1)打开SQL Server配置管理器,检查是否启用了网络配置协议下的TCP/IP。

2)在“连接”下的SQL服务器的属性下,检查是否允许允许到此服务器的远程连接。

3)检查是否可以通过SSMS连接并查询数据库。

4)在SQL Server配置管理器中检查SQL Server Browser服务是否正在运行。 (默认情况下这不会启用,也不会默认启动)。

5)如果所有这些设置,那么我会检查防火墙。

(对于任何人都可能会遇到这样的解决方案是让SQL Server和Windows身份验证)

+0

启用tcp/ip状态 –

+0

服务器允许远程连接.. –

+0

您有防火墙吗?你也可以使用该用户名/密码通过SSMS连接吗? –

0

确保您的SQL Server配置 都具有一个TCP配置,并在该IP地址配置。

您可以测试有两件事情:

  1. 关闭Windows防火墙,从SERVICES.MSC。
  2. 尝试通过IP地址,用户名,pwd连接到SQL Server, 应用程序正在尝试使用。 (打开企业管理器,连接,输入IP地址等)
0

尝试使用SQLServer的jtds驱动程序。

0

Here

并与你的代码看起来像这样安装Oracle JDBC驱动程序:

import java.sql.DriverManager; 
import java.sql.Connection; 
import java.sql.SQLException; 

public class OracleJDBC { 

    public static void main(String[] argv) { 

     System.out.println("-------- Oracle JDBC Connection Testing ------"); 

     try { 

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

     } catch (ClassNotFoundException e) { 

      System.out.println("Where is your Oracle JDBC Driver?"); 
      e.printStackTrace(); 
      return; 

     } 

     System.out.println("Oracle JDBC Driver Registered!"); 

     Connection connection = null; 

     try { 

      connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:BankSystem", "Andrew","andrei23"); 

      } 
     catch (SQLException e) { 

      System.out.println("Connection Failed! Check output console"); 
      e.printStackTrace(); 
      return; 

     } 

     if (connection != null) { 
      System.out.println("You made it, take control your database now!"); 
     } else { 
      System.out.println("Failed to make connection!"); 
     } 
    } 

} 

它可能应该工作,如果没有,什么是输出样子呢?仍然等于之前的情况?你检查过防火墙吗?连接到服务器允许?

+0

抛出java.lang.ClassNotFoundException访问:com.microsoft.sqlserver.jdbc.SQLServerDriver 和我安装ojdbc6.jar到buildpaths –

+0

会不会是你的JDBC驱动程序不你的项目的类路径?你有没有检查jdbc的偏好,就像正确的路径? – 2014-02-17 16:16:51

+0

在ojdbc6.jar中没有com.microsoft ....只有oracle.jdbc.etc –

0

据我所知,sqlserver的标准端口是1433而不是1443。