像这样存储为BLOB(请参见下面的代码示例)。我认为这可能比使用Java序列化更好,因为Java的内置序列化将需要2427字节,并且非Java应用程序将很难处理数据。也就是说,将来是否应该有任何非Java应用程序查询数据库....如果没有,则内置序列化会少一些。
public static void storeInDB() throws IOException, SQLException { double[] dubs = new double[300]; ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); for (double d : dubs) { dout.writeDouble(d); } dout.close(); byte[] asBytes = bout.toByteArray(); PreparedStatement stmt = null; // however we normally get this... stmt.setBytes(1, asBytes);}public static double[] readFromDB() throws IOException, SQLException { ResultSet rs = null; // however we normally get this... while (rs.next()) { double[] dubs = new double[300]; byte[] asBytes = rs.getBytes("myDoubles"); ByteArrayInputStream bin = new ByteArrayInputStream(asBytes); DataInputStream din = new DataInputStream(bin); for (int i = 0; i < dubs.length; i++) { dubs[i] = din.readDouble(); } return dubs; }}
编辑:我希望使用BINARY(2400),但MySQL说:
mysql> create table t (a binary(2400)) ;ERROR 1074 (42000): Column length too big for column 'a' (max = 255);use BLOB or TEXT instead
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)