Java: Connection Class Error

Asked

Viewed 128 times

1

How to fix this error ? This is preventing you from connecting to the bank....

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // Eventoos do Botao inserir
    try {
        //Registra JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Abrindo a conexão: ATENÇÃO OS DOIS PARÂMETROS VAZIOS("") SÃO USUÁRIO E SENHA, RESPECTIVAMENTE.
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/escola?zeroDateTimeBehavior=convertToNull", "", "");

        //Executa a query de inserção
        java.sql.Statement st = conn.createStatement();
        st.executeUpdate("INSERT INTO aluno (id,nome,cpf) VALUES ("
                + this.jTextFieldId.getText() + ",'"
                + this.jTextFieldNome.getText() + "','"
                + this.jTextFieldCPF.getText() + "')");

        JOptionPane.showMessageDialog(rootPane, "Aluno inserido");
    } catch (SQLException | ClassNotFoundException e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }//Fim try
}       

inserir a descrição da imagem aqui

  • 1

    I don’t understand. What’s the mistake?

  • this underlined in red line and is not connecting to the bank...

  • 1

    your IDE does not speak the error that is on the line?

  • yes...:cannot find Symbol. Symbol class: class Connection. Location Jframestudent

  • In my view, you are missing add to lib (.jar) from your database to your project, so that it finds these classes.

  • https://en.wikipedia.org/wiki/SQL_injection

  • Fixed...just import it

Show 2 more comments

2 answers

1

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// Eventoos do Botao inserir
try {
    //Registra JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    //Abrindo a conexão: ATENÇÃO OS DOIS PARÂMETROS VAZIOS("") SÃO USUÁRIO E SENHA, RESPECTIVAMENTE.
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/escola?zeroDateTimeBehavior=convertToNull", "usuario_banco", "senha_banco");

    //Executa a query de inserção
    //java.sql.Statement st = conn.createStatement();
    //st.executeUpdate("INSERT INTO aluno (id,nome,cpf) VALUES (" + this.jTextFieldId.getText() + ",'"+ this.jTextFieldNome.getText() + "','"+ this.jTextFieldCPF.getText() + "')");

    PreparedStatement pstmStatement = conn.prepareStatement("INSERT INTO aluno  VALUES  ( ? , ?  , ? ) ");// id , nome, cpf 

    pstmStatement.setInt(1, this.jTextFieldId.getText() ); //this.jTextFieldId.getText()

    pstmStatement.setString(2, this.jTextFieldNome.getText() ); // this.jTextFieldNome.getText()

    pstmStatement.setString(3,  this.jTextFieldCPF.getText() ); // this.jTextFieldCPF.getText()

    pstmStatement.executeUpdate(); //executa o SQL 


    JOptionPane.showMessageDialog(rootPane, "Aluno inserido");//

} catch (SQLException | ClassNotFoundException e) {

    JOptionPane.showMessageDialog(rootPane, e);

}//Fim try

}

0


Fixed. Just import:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Browser other questions tagged

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