How to select all the columns of a row?

-1

Is it possible to select all the 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 see if the ID of the user that I am trying to register in the database already exists.

This is so that the user does not have to wait to fill 30 fields and only when 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 appears where Yes searches ALL user data already registered in the bank and screen displays, and the No option remains on the same screen.

With this, I need to display all the columns of the ID in the first field.

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 ID 3 - I am warned that this id already exists and I click on Yes on the confirmdialog because I want to make sure this user is the same one I'm trying to register.

When I click on Yes , a SELECT will fetch in DB and display within the respective fields:

3 , 31, 13, 8888-8888.

Is this possible? If anyone can show me how to do it, I appreciate it.

I tried several things. Among them:

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

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

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

    
asked by anonymous 30.04.2015 / 02:41

2 answers

-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);
  }
}
    
04.05.2015 / 14:04
2

Yes, you can select all the columns of a row, using the * , the wildcard for all the columns of the relation, apparently you are having problems mounting the query. p>

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

So, 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));
        }
    }
}

Given 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 the definition of your table is different, just change the name of the columns in the example.

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

    
30.04.2015 / 03:54