我认为dedalo 面临的问题是他正在处理数组(我假设一个是锯齿状的,一个是多维的)并且他想将这些序列化为blob。
但是,数组不能直接序列化,所以他在问如何去做。
最简单的方法是遍历数组并按照 Dave 的建议构建一个字符串并存储该字符串。正如 duffymo 指出的那样,这将允许您从数据库中的值查看内容,而不是在需要检查数据时反序列化数据。
如果您想知道如何将数组序列化为 BLOB...(这似乎有点矫枉过正)
你可以serialize one-dimensional arrays和jagged arrays,例如:
public class Test {
public static void main(String[] args) throws Exception {
// Serialize an int[]
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"));
out.writeObject(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
out.flush();
out.close();
// Deserialize the int[]
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));
int[] array = (int[]) in.readObject();
in.close();
// Print out contents of deserialized int[]
System.out.println("It is " + (array instanceof Serializable) + " that int[] implements Serializable");
System.out.print("Deserialized array: " + array[0]);
for (int i=1; i<array.length; i++) {
System.out.print(", " + array[i]);
}
System.out.println();
}
}
至于在 MySQL 中存储什么数据类型,there are only four blob types to choose from:
The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB
选择最好的取决于序列化对象的大小。我想BLOB 就足够了。