Calculating Random Password using Arraylist and Resultset in Java

Asked

Viewed 65 times

0

Good morning, I have a question, I need to calculate a new password for each Patient when going through this method:

 ps = getConexao().prepareStatement(SQL_VERIFICAR_PROTOCOLOS_PENDENTES); 
            ps.setDate(1, new java.sql.Date(dto.getData().getTime()));
            ps.setDate(2, new java.sql.Date(dto.getData2().getTime()));
            ps.setInt(3, dto.getIdCorp());
            rs = ps.executeQuery();

            while (rs.next()) { // fazendo a Busca pelo ResultSet

           StringBuffer CalcSenha = new StringBuffer();  
           ArrayList<String> lista = new ArrayList<String>();
           lista.add(rs.getString("nome"));
           lista.add(rs.getString("data_nascimento"));
           lista.add...........(pego dados do banco);

                for (int i = 0; i < lista.size(); i++) {
                    CalcSenha.append(lista.lista.get(0));
                    System.out.println("A senha calculada foi:" + CalcSenha);
                }

            }

The calculation is made like this:

first letter of the name, position 7 of the date of birth; position 1 of date of birth; position 9 of the date of birth; position 1 of readroom; age at 3 positions; Total= 6 characters

I can get this data already with My ResultSet, my doubt is I use the StringBuffer giving append, as I go through and pick up the first letter of the Patient’s Name, the 7 position of the date of Birth...... ???
I’m not getting it, because the way it is Calcpassword.append(list.lista.get(0)); it pulls the whole name.

1 answer

1


To get only the first letter of the name, you can use the substring method().

Ex.:

for (int i = 0; i < lista.size(); i++) {
                CalcSenha.append(lista.lista.get(0).subString(0,0));
                System.out.println("A senha calculada foi:" + CalcSenha);
            }

For the date, you will need to convert it to String before, which can be done using Simpledateformat:


Date date = lista.lista.get(1);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String text = df.format(date);

  • I did it that way, I found this substring and was parsing what I needed. Thank you.

Browser other questions tagged

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