Database connection problem

Asked

Viewed 83 times

3

I am building an application from Netbeans IDE 8.1 and accessing the database developed in Sql server further when running the application this generates error:

Connection error The port number 1433/sistema_venda 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());
        }
    }
}
  • What is the name of the jar you are using for connection? The bank is created right? You can connect to it using any client ?

  • Which instance are you using? MSSQL, Sqlexpress?

  • I am using sqljdbc42.jar . This yes straight.

  • I’m using sqljdbc42.jar. And the bank is right

  • the 'and ORLANDOJJCAWEND. Sql server

2 answers

2


Using the microsoft documentation to make a connection with their JDBC, we have the following method conectar. I also put an example of the use 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;
}

0

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

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

It seems your connection would be:

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

Source, Example and parameters accepted in the connection string

Browser other questions tagged

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