/ Java

JDBC problem: ResultSet.getString returns nothing

I recently did some database development in Java using Eclipse with JDBC and MySQL. I came across something very weird that I would like to share.

At first I thought the getString method of my ResultSet instance returned empty Strings but as I started analyzing the returned content I found out that it was actually null bytes. Exactly as many as the length of the expected string.

As always I Googled the phenomena and found people with similar problem, but no solution. I found that this is actually an Eclipse thing and when I tried to run my code outside Eclipse it worked perfectly.

I finally came up with a solution, a replacement for getString that uses getAsciiStream insted.

public String getString(ResultSet result, String column) {
String str = new String();
try {
InputStreamReader in = new InputStreamReader(result
.getAsciiStream(column));
while (in.ready())
str = str + (char) in.read();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return str;
}
}