I'm working on a project in which I need to conenct the Eclipse IDE to an SQLServer database. I downloaded and imported the .jar SQLJBDC driver file in Eclipse, and put the following code in my class:
`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:
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. 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:
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!
After these changes, you will have to restart your 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
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.