简体   繁体   中英

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. 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.

Assuming that the byte array in Java has fewer than 4000 elements, you can store it in a RAW column in Oracle. This tells Oracle that the data is binary so it won't ever attempt to do character set conversion. 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.

Use a BLOB column and a PreparedStatement:

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

Then in your Java code:

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.
  2. Store the byte array into a CLOB or BLOB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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