简体   繁体   English

无法将Blob转换为字符串,SQLite + java

[英]cannot convert blob to string, SQLite+java

I used following code for retrieve data from SQLite database. 我使用以下代码从SQLite数据库检索数据。 The 4th column of database stored string as BLOB. 数据库的第四列将字符串存储为BLOB。 Now i need to get that string values back. 现在,我需要重新获得该字符串值。 But Blob blob = exe.getBlob("col_4"); 但是Blob blob = exe.getBlob("col_4"); gave me an exception not implemented by SQLite JDBC driver . 给了我一个not implemented by SQLite JDBC driver的异常。 How can i solve this ? 我该如何解决?

Connection c = null;
Statement stm = null;

String db = "C:\\Users\\Asus™\\Desktop\\New folder (2)\\tempo.db";
try {
    Class.forName("org.sqlite.JDBC");
    c = DriverManager.getConnection("jdbc:sqlite:" + db);
    c.setAutoCommit(false);

    System.out.println("DB opened successfully !");

    stm = c.createStatement();
    int rc = 0;
    try (ResultSet exe = stm.executeQuery("SELECT * FROM tblTest;")) {
        while (exe.next()) {
            Blob blob = exe.getBlob("col_4");
            String password = new String(blob.getBytes(1, (int) blob.length()));

            System.out.println(password
                    + exe.getString("col_1") + "\t"
                    + exe.getString("col_2") + "\t"
                    + exe.getString("col_3") + "\t"
                    + password
            );
            rc++;
        }
        System.out.println(rc++);
    }
    stm.close();
} catch (ClassNotFoundException ex) {
    Logger.getLogger(SQLiteBrowser.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
    Logger.getLogger(SQLiteBrowser.class.getName()).log(Level.SEVERE, null, ex);

} }

Exception : 例外情况:

java.sql.SQLException: not implemented by SQLite JDBC driver
       at org.sqlite.jdbc4.JDBC4ResultSet.unused(JDBC4ResultSet.java:320)
       at org.sqlite.jdbc4.JDBC4ResultSet.getBlob(JDBC4ResultSet.java:345)
       at sqlite.SQLiteBrowser.jButton1ActionPerformed(SQLiteBrowser.java:256)
       at sqlite.SQLiteBrowser.access$000(SQLiteBrowser.java:29)
       at sqlite.SQLiteBrowser$1.actionPerformed(SQLiteBrowser.java:76)
       at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
       at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
       at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
       at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
       at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
       at java.awt.Component.processMouseEvent(Component.java:6525)
       at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)

SQLite不支持getBlob()方法...但是支持blob ...您需要使用getBytes

byte[] bytes = exe.getBytes("col_4");

It seems, that ResultSet#getBlob method is not supported by this JDBC library implementation (due to sources , JDBC4ResultSet doesn't implement it). 看来,此JDBC库实现不支持ResultSet#getBlob方法(由于来源 ,JDBC4ResultSet没有实现它)。 You may try to use ResultSet#getBytes instead: 您可以尝试使用ResultSet#getBytes代替:

byte[] bytes = exe.getBytes("col_4");
String password = new String(bytes);

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

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