java error in connection with SQL Server

Asked

Viewed 245 times

1

I’m trying to connect to a sql server database, but I’m not succeeding. Follows connection class and test method:

public class ConexaoBanco {

    public static final String user = "sa";
    public static final String pswd = "**********"; 

    public static Connection conexao() {

        Connection con = null;
        final String jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
        final String caminho= "jdbc:microsoft:sqlserver://localhost:1433;databaseName=cliente";

        try {

            Class.forName(jdbcDriver);          
            con = DriverManager.getConnection(caminho, user, pswd);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());         
        } catch (ClassNotFoundException e) {
            System.out.println();           
        }       
        return con;
    }

    public static void main(String[] args) {
        try {
            Connection conex = ConexaoBanco.conexao();
            if (conex != null ) {
                System.out.println("conectado");
            } else {
                System.out.println("não conectado");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }               
    }
}
  • Post the mistake you’re having.

  • No error. When running, it displays "not connected", which message is programmed in the return test of the connection method.

  • Take the condition from inside the try to see the error message returned in your catch

  • I did not understand it well. I should take only the instantiation of the object from within the Try, or the if/Else as well?

  • try {
 Connection conex = ConexaoBanco.conexao();
 } catch (Exception e) {
 e.printStackTrace();
 System.out.println(e.getMessage());
 }

  • Returns nothing, console goes blank.

Show 1 more comment

1 answer

0

i have this example here that I use by default in my java applications:

final String driver = "com.mysql.jdbc.Driver";

    plogin = new JPanel();

    lnick = new JLabel("Nick: ");
    tnick = new JTextField(30);
    JLabel l1 = new JLabel("TESTE BANCO DE DADOS");
    JLabel lsenha = new JLabel("Senha: ");
    senha = new JPasswordField(30);
    blogin = new JButton("Login");
    JButton bcadastrar = new JButton("Cadastrar");
    l1.setBounds(135, 100, 450, 50);
    blogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try {
                Class.forName(driver);
                Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/testinsert", "root", "root");
                Statement stmt = conn.createStatement();

                String senha1 = String.valueOf(senha.getPassword());  // converter para string ja que o pw retorna char

                String sql = "SELECT nick FROM testinsert.cadastro where nick='" + tnick.getText() + "' and '" + senha1 + "';";

                //JOptionPane.showMessageDialog(null, tnick.getText());
                //JOptionPane.showMessageDialog(null, senha1);

                System.out.println(tnick.getText());
                System.out.println(senha1);

                ResultSet rs = stmt.executeQuery(sql);                          

                rs.next();


                //String nick = rs.getString("nick");

                if (tnick.getText() != null && senha.getPassword() != null) {
                    JOptionPane.showMessageDialog(null, "Usuário Ativo");
                    plogin.setBounds(0, 0, 0, 0);
                    menu();
                }

            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
                ex.printStackTrace();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Usuário ou Senha Inválidos");
                ex.printStackTrace();
            }
        }
    });

This would be a default login code, from which when I sign up I just change the line on which a SELECT is made, put INSERT, it’s good you see if really your java plugin, which connects to the database is working, sometimes it is good to create a new project and link again the connector (.jar) that you are using!

Browser other questions tagged

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