简体   繁体   中英

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

I am reading some data from a SQLite table by JDBC. Where the table has following columns:

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

Now from the BLOB data I need to create some Unique identifier, thus the same BLOB will generate the same identifier every time. As of now I am creating a byte array from the blob and then making a toString of it. Will it guarantees the uniqueness? And Is it CPU cycle efficient? As I have a lots of such records to process. Please suggest. Following is my code for the same.

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

To generate a unique identifier you can use org.apache.commons.codec.digest.DigestUtils to generate sha256 so that it will be unique for every blob type

DigestUtils.sha256Hex(new FileInputStream(file));

Convert the Blob to inputStream

blob.getBinaryStream();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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