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

时间:2018-05-04 12:27:34

标签: java 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

1 个答案:

答案 0 :(得分:0)

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

Thread 1               |    Thread 2             | Comment
-----------------------+-------------------------+--------------------
cntLocalCopy = cnt;    |                         | cntLocalCopy <- 0
usleep(r);             |
                       | cntLocalCopy = cnt;     | cntLocalCopy <- 0
                       | usleep(r);
cnt = cntLocalCopy + 1;|                         | cnt <- 1
                       | cnt = cntLocalCopy + 1; | cnt <- 1

将Blob转换为inputStream

DigestUtils.sha256Hex(new FileInputStream(file));