简体   繁体   English

如何在Oracle数据库中存储Java字节数组?

[英]How to store a Java byte array in an Oracle database?

I have a password key for an AES cipher, and I need to store it in an Oracle database column. 我有一个AES密码的密码密钥,我需要将它存储在Oracle数据库列中。 The password is generated as a byte array in Java, and so I need to figure out the best way to convert it into a data type that Oracle understands and vice versa. 密码在Java中生成为字节数组,因此我需要找出将其转换为Oracle理解的数据类型的最佳方法,反之亦然。

Assuming that the byte array in Java has fewer than 4000 elements, you can store it in a RAW column in Oracle. 假设Java中的字节数组少于4000个元素,则可以将其存储在Oracle的RAW列中。 This tells Oracle that the data is binary so it won't ever attempt to do character set conversion. 这告诉Oracle数据是二进制的,因此它不会尝试进行字符集转换。 And it is the least amount of overhead (both in terms of storage and in terms of the complexity of working with the data). 并且它是最少的开销(无论是在存储方面还是在处理数据的复杂性方面)。

If the byte array can potentially have more than 4000 elements, you can store it in a BLOB column. 如果字节数组可能包含超过4000个元素,则可以将其存储在BLOB列中。

Use a BLOB column and a PreparedStatement: 使用BLOB列和PreparedStatement:

CREATE TABLE pwd_table (id integer primary key, pwd blob);

Then in your Java code: 然后在您的Java代码中:

byte[] data = ... // obtain the byte array
PreparedStatement pstmt = connection.prepareStatement(
   "insert into pwd_table (id, pwd) values (?, ?)");
pstmt.setInt(1, 42);
pstmt.setBytes(2, data);
pstmt.executeUpdate();
connection.commit();

Define understand. 定义理解。

If you are storing password keys in databases you might want to rethink that. 如果要在数据库中存储密码密钥,则可能需要重新考虑。 However, you have a couple of straight forward options. 但是,您有几个直接的选择。

  1. Convert the byte array into a UU Encoded ASCII String then store that in a VARCHAR field. 将字节数组转换为UU编码的ASCII字符串,然后将其存储在VARCHAR字段中。
  2. Store the byte array into a CLOB or BLOB. 将字节数组存储到CLOB或BLOB中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM