- 您可以使用的数据类型为
BLOB
。 转换PDF文件并将
byte[]
数组保存在数据库中。private byte[] getByteArrayFromFile(final document handleddocument) throws IOException {final ByteArrayOutputStream baos = new ByteArrayOutputStream();final InputStream in = new FileInputStream(handleddocument);final byte[] buffer = new byte[500];int read = -1;while ((read = in.read(buffer)) > 0) { baos.write(buffer, 0, read);}in.close();return baos.toByteArray();}
将其插入数据库中如果使用任何ORM工具,只需将列映射为blob,该工具即可为您处理。如果您不使用它,则可以创建一个准备好的语句。语句具有一个称为setBlob()的方法,该方法将非常有用。考虑下面的示例,并使用blob列创建一个普通的插入查询。
String sql = "INSERT INTO testtable(stringcolumn, blobcolumn) VALUES(?,?)";
PreparedStatement statement = conn.getConnection().prepareStatement(sql);
statement.setLong(1, version);
ByteArrayInputStream bais = new ByteArrayInputStream(getByteArrayFromFile(document));
statement.setBlob(2, bais);
statement.execute();conn.commit();



