Contar os resgistro (tab em BD) e msotrar o total em JTextFeild ou JLabel

1 resposta
E

Boa noite!!

Como contar os registros em uma tabela dentro de uma banco em mostrar em um JTextField ou JLabel?

Eu tentei isso mas não funciona.
private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
        //Metodo para mostrar total de registros
            Connection conn = null;
            ResultSet rs = null;
            PreparedStatement pst = null;
        try{
            
            String sSql = "select count(TIM_NOME) from TIMES";
            pst = conn.prepareStatement(sSql);
            rs = pst.executeQuery();
            if(rs.next()){
                String TtlTimes = rs.getString("count(TIM_NOME)");
                jtfTtlTimes.setText(TtlTimes);
            }
    }catch(Exception e){

    }
    }

1 Resposta

pmlm

Tens de dar um nome à tua coluna

String sSql = "select count(TIM_NOME) as contador from TIMES";  

(...)

    String TtlTimes = rs.getString("contador");
Criado 12 de abril de 2015
Ultima resposta 13 de abr. de 2015
Respostas 1
Participantes 2