Connection problem with the database

3

I'm building an application from NetBeans IDE 8.1 and accessing the database developed in Sql server more when executing the application is generating the error:

  

Connection error The port number 1433 / system_vend is not valid

I already configured TCP / IP.

public class ConectaBanco {

    public Statement stm;
    public ResultSet rs;

    private final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String caminho = "jdbc:sqlserver://localhost:1433/sistema_venda";
    private final String user = "user";
    private final String senha = "password";

    public Connection conn;

    public void conexao(){
        try {
            System.setProperty("jdbc.Driver", driver);
            conn = DriverManager.getConnection(caminho, user, senha);

            JOptionPane.showMessageDialog(null, "Conectado com sucesso!");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro de conexao " + ex.getMessage());
        }
    } 
    public void desconecta(){
        try {
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro ou achar a conexao " + ex.getMessage());
        }
    }
}
    
asked by anonymous 30.01.2017 / 13:23

2 answers

2

Using Microsoft documentation to make a connection to the JDBC of them, we have the following conectar method. I've also put an example of the usage for your case:

public static void main(String[] args) {
  try {
    conectar("localhost", "ORLANDOJJCAWEND", null, "sistema_venda", "vivibd", "vivi!");
  } catch (SQLException ex) {
    Logger.getLogger(ConexaoSqlServer.class.getName()).log(Level.SEVERE, null, ex);
  }
}

public static Connection conectar(String servidor, String instancia, Integer porta, String base, String usuario, String senha) throws SQLException {
  Properties propriedades = new Properties();
  String url = "jdbc:sqlserver://";
  Connection conexao;

  if (servidor != null && !servidor.isEmpty()) {
    if (instancia != null && !instancia.isEmpty()) {
      servidor = servidor + "\" + instancia;
    }

    if (porta != null) {
      servidor = servidor + ":" + porta.toString();
    }
  } else {
    servidor = "";
  }

  propriedades.put("jdbc.Driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
  propriedades.put("databaseName", base);
  propriedades.put("user", usuario);
  propriedades.put("password", senha);

  // O formato obedecerá: jdbc:sqlserver://[serverName[\instanceName][:portNumber]]
  url = url + servidor;
  conexao = DriverManager.getConnection(url, propriedades);

  return conexao;
}
    
30.01.2017 / 13:59
0

The connection string is wrong, the connection format would be:

jdbc:microsoft:sqlserver://HOST:PORT;DatabaseName=DATABASE

It looks like your connection would be:

jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sistema_venda

Font , Example and parameters accepted in the connection string

    
30.01.2017 / 14:43