简体   繁体   English

连接SQL Server 2012-Eclipse Neon

[英]Connection SQL server 2012 - Eclipse Neon

I'm working on a project in which I need to conenct the Eclipse IDE to an SQLServer database. 我正在一个项目中,我需要将Eclipse IDE连接到SQLServer数据库。 I downloaded and imported the .jar SQLJBDC driver file in Eclipse, and put the following code in my class: 我在Eclipse中下载并导入了.jar SQLJBDC驱动程序文件,并将以下代码放入了我的类中:

`import java.sql.*;
import java.sql.DriverManager;

public class connexion {

// Méthode permettant d'enregistrer des données dans la base de données
public static void rempli() {

    String url = "jdbc:sqlserver://localhost:1433;databaseName=Tunnel" ;// le chemin vers le serveur de BD et la base de données
    String user = "sa"; // Nom d'un utilisateur de la base de données
    String pass = "*********"; // Son mot de passe
    Connection cn = null; // Déclaration d'un objet de type connection, il permet d'utiliser une méthode de connexion
    Statement st = null; // Déclaration d'un objet Statement. il permet d'envoyer des requêtes
    try {

                //Etape 1 : Chargement du driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                //Etape 2 : récupération de la connexion
            cn = DriverManager.getConnection(url, user, pass);


    }
    catch (SQLException | ClassNotFoundException e) {

        e.printStackTrace();
    }
}
public static void main(String[] args) {

    rempli();
}
}`

When I launched the program, I had the following answer after nearly 20 seconds: 启动该程序时,将近20秒后我得到以下答案:

com.microsoft.sqlserver.jdbc.SQLServerException: Échec de la connexion TCP/IP à l'hôte localhost, port 1433. Erreur : « Connection refused: connect. Vérifiez les propriétés de connexion. Assurez-vous qu'une instance de SQL Server est en cours d'exécution sur l'hôte et accepte les connexions TCP/IP au port. Vérifiez que les connexions TCP au port ne sont pas bloquées par un pare-feu. ».
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at connexion.rempli(connexion.java:21)
at connexion.main(connexion.java:32)

I deactivated my firewall, authorized access on the different ports TPC and I don't really know what to do more now. 我停用了防火墙,在不同端口TPC上进行了授权访问,我真的不知道现在该怎么做。 Can anyone help me? 谁能帮我?

Thank you for the help! 感谢您的帮助!

If you have already configured your MS SQL server to listen on TCP/IP traffic, then it could be any of these reasons: 如果已经将MS SQL服务器配置为侦听TCP / IP通信,则可能是以下原因之一:

  • Firewall is blocking the incoming connection 防火墙阻止传入连接
  • SQL SERVER is not running on the host SQL SERVER不在主机上运行
  • The port you have configured is not correct, ie its not 1433 您配置的端口不正确,即不是1433

If you've validated that the issue doesn't fall into the above categories, then you may try doing this to enable port 1433 for TCP/IP traffic! 如果您已确认问题不属于上述类别,则可以尝试这样做以为TCP / IP流量启用端口1433!

  • Go to Start -> All Programs -> Microsoft SQL Server 2008/2012/2014 -> Configuration Tools 转到开始->所有程序-> Microsoft SQL Server 2008/2012/2014->配置工具
  • Click SQL Server Configuration Manager 单击“ SQL Server配置管理器”
  • Expand SQL Server Network Configuration-> Protocol 展开SQL Server网络配置->协议
  • Enable TCP/IP Right box 启用TCP / IP权限框
  • Double Click on TCP/IP and go to IP Addresses Tab and add 1433 under TCP port. 双击TCP / IP并转到IP地址选项卡,然后在TCP端口下添加1433。

After these changes, you will have to restart your SQL Server Services > SQL Server 完成这些更改后,您将必须重新启动SQL Server Services > SQL Server

If these are not helping then you may consider adding the following as well: 如果这些方法没有帮助,则可以考虑添加以下内容:

System.setProperty("java.net.preferIPv4Stack", "true");

Or to pass it as a VM option: -Djava.net.preferIPv4Stack=true 或将其作为VM选项传递: -Djava.net.preferIPv4Stack=true

Hope this helps! 希望这可以帮助!

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

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