繁体   English   中英

如何从BLOB数据类型SQLite JDBC创建唯一标识符

[英]How to create unique identifier from a BLOB data type SQLite JDBC

我正在通过JDBC从SQLite表中读取一些数据。 该表具有以下列:

[Id:Integer], [parentId:Integer], [Name:String], [Type:Integer], [Data:BLOB]

现在,我需要从BLOB数据中创建一些唯一标识符,因此相同的BLOB每次都会生成相同的标识符。 到目前为止,我正在从Blob创建一个字节数组,然后对其进行toString 会保证唯一性吗? CPU周期效率高吗? 因为我有很多这样的记录要处理。 请提出建议。 以下是我的相同代码。

public static void scanData(String dbName) {
    String url = "jdbc:sqlite:C:/dbfolder/" + dbName;
    try (Connection con = DriverManager.getConnection(url);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from someTable");) {

        while (rs.next()) {
            Integer type = rs.getInt("Type");
            if (type != null && type.equals(3)) {
                Integer rowId = rs.getInt("Id");
                Integer parentId = rs.getInt("ParentID");
                String name = rs.getString("Name");
                System.out.println("rowId = " + rowId);
                System.out.println("parentId = " + parentId);
                System.out.println("name = " + name);
                System.out.println("type = " + type);

                InputStream is = rs.getBinaryStream("Data");
                if (is != null) {
                    byte[] arr = IOUtils.toByteArray(is);
                    if (arr != null) {
                        System.out.println("Data = " + arr.toString());
                    }
                }
                System.out.println("---------------------------");
            }

        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }
}

** IOUtils is used of : org.apache.commons.io.IOUtils

要生成唯一标识符,您可以使用org.apache.commons.codec.digest.DigestUtils生成sha256,以便它对于每种Blob类型都是唯一的

DigestUtils.sha256Hex(new FileInputStream(file));

将Blob转换为inputStream

blob.getBinaryStream();

暂无
暂无

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

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