Error connecting to sql server database with java and problem with connection string

2

I put the jar and did the build path. I've been trying for some time to connect to the microsoft bank but it 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);

    }
}

}

In the database server settings, I left tcp enabled.

    
asked by anonymous 25.02.2016 / 04:58

2 answers

3

The problem was the sql connection string that was incomplete, this is the correct way:

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

I was able 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());  

    }
  }
}
    
25.02.2016 / 16:53
5

I'll list a number of causes that may come in your problem, I hope at least one will help you.

  • I'm guessing to set 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 who installs the express version of the bank usually comes as default MSSQLSERVER .
  • Another thing is: do you only have SQL Server 2014 running on your machine? If you have more than one, it will conflict on port 1433 and then you will not be able to connect at all without setting up to enable only one at a time.
  • Open the port in SQL Server is kind of stupid because you do it in two points, so just to make sure you actually opened the door, here's the step by step:

1- In the Configuration Manager, go to "SQL Server Network Configuration", open the item that is of your service, double-click the TCP / IP item, and pass Enabled to Yes.

2-Nowonthesecondtab,gototheendandmakesuretheportissetto1433.

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

    
25.02.2016 / 11:48