Query in database returns incorrect data

Asked

Viewed 68 times

0

Hello! I am making a query in the database and it is returning the incorrect data (as shown in the image). It is returning the class path within the package.inserir a descrição da imagem aqui

Below is the code of the class (for those who want to go exactly where they do the query, and towards the end, in the function "sqlPesquisarCargos").

package sistema.telas;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.System.Logger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

// import com.mysql.cj.x.protobuf.MysqlxNotice.Warning.Level;

import sistema.BancoDeDados;
import sistema.entidades.Cargo;

public class CargosConsultar extends JPanel{

    Cargo cargoAtual;
    JLabel labelTitulo, labelCargo;
    JTextField campoCargo;
    JButton botaoPesquisar, botaoEditar, botaoExcluir;
    DefaultListModel<Cargo> listasCargosModelo = new DefaultListModel();
    JList<Cargo> listaCargos;

    public CargosConsultar()
    {
        criarComponentes();
        criarEventos();
    }

    public void criarComponentes()
    {
        setLayout(null);

        labelTitulo = new JLabel("Consulta de cargos", JLabel.CENTER);
        labelTitulo.setFont(new Font(labelTitulo.getFont().getName(), Font.PLAIN, 20));     
        labelCargo = new JLabel("Nome do Cargo", JLabel.LEFT);

        campoCargo = new JTextField();

        botaoPesquisar = new JButton("Pesquisar Cargo");
        botaoEditar = new JButton("Editar Cargo");
        botaoEditar.setEnabled(false);
        botaoExcluir = new JButton("Excluir Cargo");
        botaoExcluir.setEnabled(false);

        listasCargosModelo = new DefaultListModel();

        listaCargos = new JList();
        listaCargos.setModel(listasCargosModelo);
        listaCargos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        // setBounds(x, y, width, height);
        labelTitulo.setBounds(200, 20, 400, 50);
        labelCargo.setBounds(150, 120, 400, 20);
        campoCargo.setBounds(150, 140, 400, 40);
        botaoPesquisar.setBounds(560, 140, 130, 40);
        listaCargos.setBounds(150, 200, 400, 240);
        botaoEditar.setBounds(560, 360, 130, 40);
        botaoExcluir.setBounds(560, 400, 130, 40);

        add(labelTitulo);
        add(labelCargo);
        add(campoCargo);
        add(botaoPesquisar);
        add(listaCargos);
        add(botaoEditar);
        add(botaoExcluir);

        setVisible(true);

    }

    public void criarEventos()
    {

        botaoPesquisar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sqlPesquisarCargos(campoCargo.getText());
            }

        });

        botaoExcluir.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // sqlDeletarCargo();
            }

        });

        listaCargos.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                cargoAtual = listaCargos.getSelectedValue();

                if(cargoAtual == null) {
                    botaoEditar.setEnabled(false);
                    botaoExcluir.setEnabled(false);
                }else {
                    botaoEditar.setEnabled(true);
                    botaoExcluir.setEnabled(false);                 
                }
            }

        });

    }

    private void sqlPesquisarCargos(String nome)
    {
        Connection conexao;
        Statement instrucaoSQL;
        ResultSet resultados;

        try {

            try {
                Class.forName(BancoDeDados.JDBC_DRIVER);
            }catch(ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("Erro: "+e.getMessage());
            }

            conexao = DriverManager.getConnection(BancoDeDados.stringDeConexao, BancoDeDados.usuario, BancoDeDados.senha);

            instrucaoSQL = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

            // resultados = instrucaoSQL.executeQuery("SELECT * FROM cargos");
            resultados = instrucaoSQL.executeQuery("SELECT * FROM cargos WHERE nome_ca LIKE '%"+nome+"%'");

            listasCargosModelo.clear();

            while(resultados.next())
            {
                Cargo cargo = new Cargo();
                cargo.setId(resultados.getInt("id_ca"));
                cargo.setNome(resultados.getString("nome_ca"));             
                listasCargosModelo.addElement(cargo);           
            }

        }catch(SQLException ex){
            JOptionPane.showMessageDialog(null, "Erro ao realizar consulta, verifique o log de erros!");
            System.out.println("Erro: "+ex.getMessage());           
            // Logger.getLogger(CargosConsultar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

1 answer

2


The database data is probably being returned correctly, they should only be presented incorrectly on the screen.

Overwrites the toString of your class Cargo to express the desired value:

public class Cargo {
...

    @Override
    public String toString() {
        return nome;
    }
}

Then the nome will be presented on your screen.

The method toString aims to return a representation of the object in String, and can be customized accordingly. Several implementations call this method.

Check the documentation here

Ideal for these problems is you debug and check where the problem actually is.

  • It worked perfectly! I would never have solved it without your help, thank you!

Browser other questions tagged

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