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;
}
Output:
Info: Conexão aberta!
Info: InputStreamReader: 12312312 getString: getInt: 1
Info: InputStreamReader: teresop getString: getInt: 2