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

时间:2015-10-06 10:53:15

标签: java sqlite

我使用以下代码从SQLite数据库中检索数据。数据库的第4列将字符串存储为BLOB。现在我需要恢复字符串值。但是Blob blob = exe.getBlob("col_4");给了我一个例外not implemented by SQLite JDBC driver。我该如何解决这个问题?

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);

}

例外:

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)

2 个答案:

答案 0 :(得分:0)

SQLite不支持

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

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

答案 1 :(得分:0)

看来,这个JDBC库实现不支持ResultSet#getBlob方法(由于sources,JDBC4ResultSet没有实现它)。您可以尝试使用ResultSet#getBytes代替:

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