Help to fill jTable

Asked

Viewed 290 times

2

I’m filling out a jtable with the birthday kids who are registered in a database.

  public void jTablePop()
{
    javax.swing.table.DefaultTableModel dtm2 = (javax.swing.table.DefaultTableModel)jTable1.getModel();  
    dtm2.setNumRows(0);  
    //ISSO TIRA AS LINHAS DA TABELA  
    int x=0;

    try{
    conexao.Conectar();

    conexao.sql = "select cli_nome,cli_cpf,cli_data_nascimento from contrato where  id_cli > 1 ";
    conexao.ps = conexao.con.prepareStatement(conexao.sql);
    conexao.rs = conexao.ps.executeQuery();

    while(conexao.rs.next())
    {
        //codigo
        dtm2.addRow(new Object[]{" ","  "," "}); 
        jTable1.setValueAt(conexao.rs.getString(1),x,0);  
        jTable1.setValueAt(conexao.rs.getString(2),x,1);  
        jTable1.setValueAt(conexao.rs.getString(3),x,2); 
        x++;
    }


    }catch(SQLException ex){
           JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "ERRO!", JOptionPane.ERROR_MESSAGE);
       }
}

But I need that in jTable appear only the birthday of the month, how could I do it ? I’ve managed to date a variable this way :

public void kappa()
    {
        SimpleDateFormat sdf1= new SimpleDateFormat("dd/MM/yyyy"); 
        Date y=new Date(); 
        String keppo = sdf1.format(y).toString();

    }

How could I do to display on jTable only the birthday of the month ? could help me ?

  • First take the current month using the Calendar library, then you convert your birth date into Calendar and take only the month as well. Finally makes an if inside that while checking whether the months are equal.

1 answer

0


I was able to do so, if someone reading it has the same doubt. Here is the solution :

 public void jTablePop()
    {

        javax.swing.table.DefaultTableModel dtm2 = (javax.swing.table.DefaultTableModel)jTable1.getModel();  
        dtm2.setNumRows(0);  

        int x=0;

        try{

        conexao.Conectar();
        Calendar c1= Calendar.getInstance();
        String hora1 = c1.get(Calendar.DAY_OF_MONTH)+"/"+(c1.get(Calendar.MONTH)+1);
        se.setText(hora1);

        conexao.sql = "SELECT cli_nome,tel,cli_data_nascimento FROM contrato WHERE LEFT(cli_data_nascimento,5) =?";
        conexao.ps = conexao.con.prepareStatement(conexao.sql);
        conexao.ps.setString(1,se.getText());
        conexao.rs = conexao.ps.executeQuery();

        while(conexao.rs.next())
        {
            //codigo
            dtm2.addRow(new Object[]{" ","  "," "}); 
            jTable1.setValueAt(conexao.rs.getString(1),x,0);  
            jTable1.setValueAt(conexao.rs.getString(2),x,1);  
            jTable1.setValueAt(conexao.rs.getString(3),x,2); 
            x++;
        }

        }catch(SQLException ex){
               JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "ERRO!", JOptionPane.ERROR_MESSAGE);
           }
    }

Browser other questions tagged

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