Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList到Byte,反之亦然_Java_Database_Arraylist_Type Conversion - Fatal编程技术网

Java ArrayList到Byte,反之亦然

Java ArrayList到Byte,反之亦然,java,database,arraylist,type-conversion,Java,Database,Arraylist,Type Conversion,嗨,我需要在我的项目的数据库中存储ArrayList,为此,我需要一些建议,通过一些技术将ArrayList存储到数据库中,如ArrayList到Byte[],将Byte[]存储在DB中,并检索我的Byte[]从DB转换为Java中的原始ArrayList,我假设您正在谈论Java的ArrayList(您没有提到您心目中的语言) 事实上,有一个类似的问题: 只需序列化一个列表,并将其插入BLOB列。然后您可以从数据库中获取它并对其进行反序列化 编辑: 这是一段可用于序列化ArrayList的代码

嗨,我需要在我的项目的数据库中存储
ArrayList
,为此,我需要一些建议,通过一些技术将
ArrayList
存储到数据库中,如ArrayList到Byte[],将Byte[]存储在DB中,并检索我的Byte[]从DB转换为Java中的原始
ArrayList

,我假设您正在谈论Java的ArrayList(您没有提到您心目中的语言)

事实上,有一个类似的问题:

只需序列化一个列表,并将其插入BLOB列。然后您可以从数据库中获取它并对其进行反序列化

编辑:

这是一段可用于序列化ArrayList的代码:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try { 
        ObjectOutputStream ooStream = new ObjectOutputStream(baos);
        ooStream.writeObject(myList);
        ooStream.close();
    } catch(IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    byte[] serializedData = baos.toByteArray();
将数据设置为byte[],现在可以将其存储在bytea/BLOB列中

我希望这有帮助