Error to connect to sql server 2014 database with java and connection string problem

Asked

Viewed 5,103 times

2

I put the jar and made the build path . I’ve been trying for a while to connect with the microsoft bank but gives this error:

Exception in thread "main" java.lang.RuntimeException:     com.microsoft.sqlserver.jdbc.SQLServerException: Falha na conexão TCP/IP com o host localhost, porta 1433. Erro: "Connection refused: connect. Verifique as propriedades da conexão. Verifique se uma instância do SQL Server está sendo executada no host e se está aceitando conexões TCP/IP na porta. Verifique se as conexões TCP na porta não foram bloqueadas por um firewall.".
at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:16)
at DAO.ContatoDAO.<init>(ContatoDAO.java:16)
at br.com.caelum.jdbc.teste.TestaConexao.main(TestaConexao.java:22)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Falha na conexão      TCP/IP com o host localhost, porta 1433. Erro: "Connection refused: connect.      Verifique as propriedades da conexão. Verifique se uma instância do SQL Server   está sendo executada no host e se está aceitando conexões TCP/IP na porta.  Verifique se as conexões TCP na porta não foram bloqueadas por um firewall.".
at   com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at  com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at br.com.caelum.jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:14)


... 2 more

The Connectionfactory class

package br.com.caelum.jdbc;

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

public class ConnectionFactory {

private String conexao = "jdbc:sqlserver://localhost:1433;databaseName=fj21";

public Connection getConnection(){

    try {
        return DriverManager.getConnection(conexao, "sa","");
    } catch (SQLException e) {
         throw new RuntimeException(e);

    }
}

}

I tweaked the database server settings and left tcp enabled.

  • Did you install this SQL? As far as I know SQL does not allow blank password for SA

  • @Krismorte can disable the "password policy", there is nothing else in it, besides that her problem is with generating the connection, the bank does not even respond.

  • I installed with password yes @Krismorte removed pq was kind of beast password...

  • Have you disabled the firewall? Or added the port in the 1433 list as an exception?

  • It wasn’t the firewall, no. It was the same connection string...

  • Before asking the question I included port 1433 yes, there on tcp/ip...that wasn’t it.

  • 1

    Aline, when you find the solution to your question yourself, you can post it as an answer right below. I suggest you edit the question by removing the answer, and post as the answer normally, so it conforms to the rules of the site.

Show 2 more comments

2 answers

6

I’ll list several causes that might come up in your problem, hopefully at least one will suit you.

  • I guess I just had to define the instanceName in the JDBC URL. It is the name of the SQL Server service. Opening the services.msc, and looking for the bank service you find a text in parentheses, for those who install the express version of the bank usually comes as standard MSSQLSERVER.
  • Another thing is: you only have SQL Server 2014 running on your machine? If there is more than one, it conflicts on port 1433 and then you will not be able to connect at all without setting to leave only one enabled at a time.
  • Opening the port in SQL Server is kind of "stupid" because you do it in two points, so, just to make sure you really opened the door stay here step by step:

1- In Configuration Manager, go to "SQL Server Network Configuration", open the item that belongs to your service, double-click the TCP/IP item, and pass Enabled to Yes.Primeira Configuração

2- Now in the second tab, go to the end and check that the port is set to 1433.inserir a descrição da imagem aqui

3- In the part that takes the name of the driver ODBC(SQL Native Client..., in your case I think is 12.0), just follow the same steps of step 1, and again ensure that the Default Port is 1433 also.

  • Guys, I got it solved. I put the connection string like this: Connected private string = "jdbc:sqlserver://localhost:1433;user=sa;password=123;databaseName=fj21";

  • And I changed the connectionFactory. I’ll post.

3


The problem was the sql connection string which was incomplete, this is the correct form:

private String conexao = "jdbc:sqlserver://localhost:1433;user=sa;password=123;databaseName=fj21";

I managed to solve the problem as follows:

package factory;

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

 public class ConnectionFactory {

  private String conexao = "jdbc:sqlserver://localhost:1433;user=sa;password=123;databaseName=fj21";
  private  String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver" ;

  public Connection getConnection() throws SQLException{

    try {
        Class.forName(DRIVER );  
        return DriverManager.getConnection(conexao);
    } catch (ClassNotFoundException e) {  
        throw new SQLException(e.getMessage());  

    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.