Java: Connection Class Error [closed]

1

How to correct 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
}       

    
asked by anonymous 23.11.2015 / 14:48

2 answers

0

Fixed. Just import:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
    
23.11.2015 / 15:11
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

}

    
23.11.2015 / 20:27