0

I'm trying to get data from MySQL database but getString always returns empty although getInt returns the value in the database. I don't get any Exception. I googled for my issue and found InputStreamReader solution which worked but I really want to make getString work on my code...

My code

    public static List<Cliente> getLista() {
    ArrayList<Cliente> arr = new ArrayList<>();
    try(Connection connection = new ConnectionFactory().getConnection();
        PreparedStatement stmt = 
                connection.prepareStatement("select * from clientes");){
        System.out.println("Conexão aberta!");
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            Cliente cliente = new Cliente();
            //cliente.setId(rs.getInt("id"));
            //cliente.setNome(rs.getString("nome"));
            //cliente.setEndereco(rs.getString("endereco"));
            //cliente.setTelefone(rs.getString("telefone"));
            String str = new String();  
            InputStreamReader in = new InputStreamReader(rs.getAsciiStream("endereco"));
            while (in.ready())  
                str = str + (char) in.read();                 
            System.out.println("InputStreamReader: "+str+" getString: "+rs.getString("endereco")+" getInt: "+rs.getInt("id"));
            //Add to List
            arr.add(cliente);
        }
        return arr;
    }catch(SQLException e){ System.out.println("Exceção SQL");
    }catch(Exception e){  System.out.println("Exceção no código!");
    }
    return null;
}

Database: enter image description here

Output:

Info:   Conexão aberta!
Info:   InputStreamReader: 12312312 getString:  getInt: 1
Info:   InputStreamReader: teresop getString:  getInt: 2
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Eduardo
  • 99
  • 2
  • 11
  • Can you try [getBytes()](https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBytes(java.lang.String)) and see if that makes any difference? – Darshan Mehta May 17 '17 at 22:57
  • 2
    Don't use *both* `getAsciiStream()` and `getString()` on the same column. Pick one and stick with it. `getAsciiStream()` has likely *consumed* the content, so there is nothing for `getString()` to read. – Andreas May 17 '17 at 22:58

0 Answers0