[我计划使用作为Blob存储在数据库中的公共密钥来加密字符串。我已经创建了一种名为readBlob()
的方法来读取密钥并将其保存到文件中。
public static void readBlob(int userid, String filename) throws SQLException, IOException {
String url = "jdbc:mysql://localhost:3306/bank";
String user = "root";
String password = "root";
String email=null;
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from users where user_id=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setLong(1, userid);
ResultSet result = statement.executeQuery();
File file = new File(filename);
@SuppressWarnings("resource")
FileOutputStream output = new FileOutputStream(file);
String keys=null;
while(result.next()) {
byte[] buffer = new byte[1];
InputStream input = result.getBinaryStream("key");
while (input.read(buffer) > 0) {
output.write(buffer);
}
}
}
它确实很好用,但是我不需要将其保存到本地文件中,我需要一种无需保存即可读取Blob的方法。