Java Tutorial/Database/Long Text
Версия от 17:44, 31 мая 2010; (обсуждение)
Get a Long Text Column/Field from a Database
public static String getLargerString(ResultSet rs, int columnIndex) throws SQLException {
InputStream in = null;
int BUFFER_SIZE = 1024;
try {
in = rs.getAsciiStream(columnIndex);
if (in == null) {
return "";
}
byte[] arr = new byte[BUFFER_SIZE];
StringBuffer buffer = new StringBuffer();
int numRead = in.read(arr);
while (numRead != -1) {
buffer.append(new String(arr, 0, numRead));
numRead = in.read(arr);
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
throw new SQLException(e.getMessage());
}
}
How do you Store a Long Text Field in a Database?
public static void setLongString(PreparedStatement pstmt, int parameterIndex, String data)
throws Exception {
// possibly a long string
pstmt.setAsciiStream(parameterIndex, new ByteArrayInputStream(data.getBytes()), data.length());
}