我试图从MySQL数据库获取数据但getString总是返回空,尽管getInt返回数据库中的值。我没有得到任何例外。我搜索了我的问题并发现InputStreamReader
解决方案有效,但我真的想让getString在我的代码上工作......
我的代码
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;
}
输出:
Info: Conexão aberta!
Info: InputStreamReader: 12312312 getString: getInt: 1
Info: InputStreamReader: teresop getString: getInt: 2