gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:53:41 26 4
gpt4 key购买 nike

我使用以下代码从 SQLite 数据库检索数据。数据库的第 4 列将字符串存储为 BLOB。现在我需要取回该字符串值。但是 Blob blob = exe.getBlob("col_4"); 给了我一个异常 未由 SQLite JDBC 驱动程序实现。我该如何解决这个问题?

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)

最佳答案

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

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

关于java - 无法将 blob 转换为字符串,SQLite+java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com