java error in connection with SQL Server

1

I am trying to connect to a sql server database, but I am not succeeding. Here is the 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());
        }               
    }
}
    
asked by anonymous 03.11.2017 / 18:36

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, of which when I am going to register I only change the line where a SELECT is done, I put INSERT, you should see if your java plugin, which connects to the bank is working, sometimes it's good to create a new project and link back to the (.jar) connector you're using!

    
04.11.2017 / 06:51