Erro no executeUpdate()

3 respostas
A

o q tem de errado nesse codigo ??

try {

//procura por uma classe no projeto

Class.forName(com.mysql.jdbc.Driver);
Connection conn;
        //conexão
         conn = DriverManager.getConnection("jdbc:mysql://localhost/teste", "root", "drhank1234");

         //Executa a query de inserção
        String query = "INSERT INTO teste.cadastrados (nome_cliente, telefone, celular, `RG`, `CPF`, cidade, rua, numero, bairro, `Email`)VALUES (?,?,?,?,?,?,?,?,?,?)";
        
        PreparedStatement stmt = conn.prepareStatement(query);
        
        stmt.setString(1, txtN.getText());
        stmt.setString(2, txtT.getText());
        stmt.setString(3, txtCel.getText());
        stmt.setString(4, txtR.getText());
        stmt.setString(5, txtC.getText());
        stmt.setString(6, txtCi.getText());
        stmt.setString(7, txtRu.getText());
        stmt.setString(8, txtN.getText());
        stmt.setString(9, txtB.getText());
        stmt.setString(10, txtE.getText());
        

        stmt.executeUpdate();
        
        stmt.close();
        conn.close();

    } catch (ClassNotFoundException ex) {
        System.out.println("bugo");
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(rootPane, " Não foi posivel efetuar o cadastro");
        System.out.println("bugo d novo");
    }

POR FAVOR ME AJUDEM !!!

3 Respostas

Vodga

troca
stmt.executeUpdate();
por
stmt.execute();

------------------------------------------------------- edit ---------------------------------------
você pode criar uma Classe ConnectionUtil

private static Connection connection;
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost/teste";
    private static final String user = "root";
    private static final String password = "drhank1234";

public static Connection open(){
    try {
        Class.forName(driver).newInstance();

        connection = DriverManager.getConnection(url, user, password);
        connection.setAutoCommit(false);

        return connection;
    } catch (ClassNotFoundException exception) {
        System.out.println("Driver de conexão não encontrado!"  + exception.getMessage());
    } catch (SQLException exception) {
        System.out.println("Não foi possível realizar a conexão com o banco de dados!" + exception.getMessage());
    } catch (InstantiationException ex) {
        System.out.println("erro ao instanciar o driver "+ ex.getMessage());
    } catch (IllegalAccessException ex) {
         System.out.println("erro ao acessar o driver "+ ex.getMessage());
    }

    return null;
}

public static void close(Connection connection) {
    try {
        connection.commit();
        connection.close();
    } catch (SQLException e) {
        System.out.println("Não foi possivel fechar a conexão!");
        e.printStackTrace();
    }
}

e usar um metodo salvar

public void salvar() {
    Connection connection;
    PreparedStatement statement;
    String save = "INSERT INTO cadastrados (nome_cliente, telefone, celular, RG, CPF, cidade, rua, numero, bairro, Email) VALUES (?,?,?,?,?,?,?,?,?,?)";
    try {
        connection = ConnectionUtil.open();
        statement = connection.prepareStatement(save);
        statement.setString(1, txtN.getText());
        statement.setString(2, txtT.getText());
        statement.setString(3, txtCel.getText());
        statement.setString(4, txtR.getText());
        statement.setString(5, txtC.getText());
        statement.setString(6, txtCi.getText());
        statement.setString(7, txtRu.getText());
        statement.setString(8, txtN.getText());
        statement.setString(9, txtB.getText());
        statement.setString(10, txtE.getText());
        statement.execute();
        statement.close();
        ConnectionUtil.close(connection);
    } catch (SQLException exception) {
        System.out.println("erro " + exception);
    }
}

Não esqueça de Adicionar o “Driver JDBC do MySQL” em bibliotecas!

guivirtuoso

Qual o erro que está dando?

A

cara deu certo. valeu mesmo brother :+1:

Criado 5 de janeiro de 2016
Ultima resposta 6 de jan. de 2016
Respostas 3
Participantes 3