利用java代码将mysql的blob转化为String类型
时间: 2024-05-11 15:15:03 浏览: 128
可以使用以下代码把 MySQL 的 blob 转成字符串:
```
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
ps = conn.prepareStatement("SELECT content FROM mytable WHERE id = ?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("content");
byte[] bytes = blob.getBytes(1, (int) blob.length());
String content = new String(bytes, "UTF-8");
System.out.println(content);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
阅读全文