How to select all columns of a row?

Asked

Viewed 1,112 times

-1

It is possible to select all columns of a row?

I have a function where, on the screen where the user will be created, a validation is made in the first field typed to verify if the ID user I am trying to register in the bank already exists.

This, so that the user does not have to wait to fill 30 fields and just by clicking create, be warned that the user in question already exists.

When I type the ID of the existing user and move to the next field, a confirmDialog where the Yes searches ALL user data already registered in the database and shows on screen, and the option Not remains on the same screen.

With that, I need that, all the columns of the ID typed in the first field, are displayed.

cd_telefone|cd_cliente  |nr_ddd |nr_telefone
    1      | 30         | 11    |2562-2791
    2      | 30         | 11    |2562-2791
    3      | 31         | 13    |8888-8888
    4      | 30         | 11    |5555-5555
    5      | 30         | 13    |9623-54002
    6      | 30         | 11    |1111-2525

Example: I typed the ID 3 - I am warned that this id already exists and I click on Yes in the confirmdialog because I want to make sure that this user is the same one that I’m trying to register.

When I click on Yes, one SELECT get in the DB and displays within the respective fields:

3 , 31, 13, 8888-8888.

Is that possible? If anyone can show me how to do I thank you.

I tried several things. Among them:

SELECT * FROM userinfo WHERE ID '"+IdTextField+"';

And several other ways, but none of them had the effect I need.

PS: I’m trying to display the data by SELECT, on the same screen I typed the ID existing.

  • 1

    Didn’t you mean: SELECT * FROM userinfo WHERE ID = '"+Idtextfield+"' ? You’d better edit your question and make a better explanation, she’s very confused.

  • Do you want to check while filling out the form for any user with any of the information being typed as soon as the user changes the field? That’s it?

  • Post the code that didn’t work.

  • That’s exactly what @Erloncharles said!

  • You just need to retrieve all the columns of a listed tuple or something else? If anything else, talk concretely, what? If you are just going to retrieve the columns, see the answer below, please.

2 answers

2

Yes, it is possible to select all columns in a row using the * even, the wildcard for all columns of the relation, apparently you are having problems in the assembly of the query.

For SELECT * FROM userinfo WHERE ID '"+IdTextField+"'; implies that you are concatenating the fields to filter in the query. Give preference in the use of Preparedstatements.

Then, based on the definition of your table, with the fields cd_telefone, cd_cliente, nr_ddd, nr_telefone you can do something like this:

final String sql = "SELECT * FROM userinfo WHERE cd_cliente = ?;";
final String format = "cd_telefone: %s | cd_cliente: %d | nr_ddd: %d | nr_telefone: %s";

try (final Connection conn = /* obtenha a conexão de alguma forma */; final PreparedStatement ps = conn.prepareStatement(sql)) {
    ps.setLong(1, 30); // o '30' é o filtro, no nosso exemplo, para 'cd_cliente'

    try (final ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            final Long cdTelefone = rs.getLong("cd_telefone");
            final Long cdCliente = rs.getLong("cd_cliente");
            final Integer nrDDD = rs.getInt("nr_ddd");
            final String nrTelefone = rs.getString("nr_telefone");
            System.out.println(String.format(format, cdTelefone, cdCliente, nrDDD, nrTelefone));
        }
    }
}

Considering the data you entered in the question, this example will generate the following output:

cd_telefone: 1 | cd_cliente: 30 | nr_ddd: 11 | nr_telefone: 2562-2791
cd_telefone: 2 | cd_cliente: 30 | nr_ddd: 11 | nr_telefone: 2562-2791
cd_telefone: 4 | cd_cliente: 30 | nr_ddd: 11 | nr_telefone: 5555-5555
cd_telefone: 5 | cd_cliente: 30 | nr_ddd: 13 | nr_telefone: 9623-54002
cd_telefone: 6 | cd_cliente: 30 | nr_ddd: 11 | nr_telefone: 1111-2525

To generate the result for the ID(cd_telefone) 3 or if your table definition is different, just change the column name in the example.

Note: only use the * when really necessary and in tables with few columns, use in cases you do not need information of all columns brings problems like those described here.

-1


SOLUTION!

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.sql.*;

public class Estudos extends JFrame{
  public Estudos(){
    super("JTable");

    final DefaultTableModel modelo = new DefaultTableModel();

    // constrói a tabela
    JTable tabela = new JTable(modelo);

    // Cria duas colunas
    modelo.addColumn("Código");
    modelo.addColumn("Nome");
    modelo.addColumn("Senha");
    modelo.addColumn("Idade");

    // exibe os dados da tabela MySQL
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/usuarios?user=root&password=Schindler88");

      // procedimentos para obter os dados de uma tabela
      Statement stmt = conn.createStatement();
      String query = "SELECT * FROM USUARIOS";
      ResultSet rs = stmt.executeQuery(query);

      while(rs.next()){ 
        int id = rs.getInt("CODIGO");
        String nome = rs.getString("NOME");
        String senha = rs.getString("SENHA");
        int idade = rs.getInt("IDADE");
        modelo.addRow(new Object[]{new Integer(id), nome, senha, new Integer(idade)});
      }

      // fim procedimento para obter os dados
      } 
      catch(SQLException ex){
           System.out.println("SQLException: " + ex.getMessage());
           System.out.println("SQLState: " + ex.getSQLState());
           System.out.println("VendorError: " + ex.getErrorCode());
      }
      catch(Exception e){
        System.out.println("Problemas ao tentar conectar com o banco de dados");    
    }
    // fim MySQL

    tabela.setPreferredScrollableViewportSize(new Dimension(350, 50));

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JScrollPane scrollPane = new JScrollPane(tabela);
    c.add(scrollPane);

    setSize(400, 300);
    setVisible(true);
  }

  public static void main(String args[]){
    Estudos app = new Estudos();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Browser other questions tagged

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