无法将blob转换为字符串、SQLite+;JAVA
我使用以下代码从SQLite数据库检索数据。数据库的第4列将字符串存储为BLOB。现在我需要找回字符串值。但是无法将blob转换为字符串、SQLite+;JAVA,java,sqlite,Java,Sqlite,我使用以下代码从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 { Cla
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)
SQLite不支持getBlob()
方法。。。但是BLOB是受支持的。。。您需要使用getBytes
:
byte[] bytes = exe.getBytes("col_4");
这个JDBC库实现似乎不支持ResultSet#getBlob
方法(因为,JDBC4ResultSet没有实现它)。您可以尝试改用ResultSet#getBytes
:
byte[] bytes = exe.getBytes("col_4");
String password = new String(bytes);
String密码=新字符串(字节)代码>返回无法识别的字符。比如<代码>Ќ����z
。您可以尝试创建一个blob back并尝试将其转换为字符串blob blob=c.createBlob();blob.setBytes(1,字节);但不确定是否有帮助,请尝试检查编码。如何才能以正确的格式(不是无法识别的字符,Ќ)获取字节作为字符串
����z、 –
)?类似于String str=新字符串(字节,“UTF-8”)代码>。。。或者使用ApacheCommons库:String str=IOUtils.toString(inputStream,“UTF-8”)代码>为什么您认为密码字段未加密?