English 中文(简体)
SQLite中更新语句的问题
原标题:problems with update statement in SQLite
  • 时间:2011-05-31 15:06:48
  •  标签:
  • java
  • sqlite

I have created a database using SQLite. I want to update the value of a "features" column( type Blob)...but i do not know how to write the "update" statement . This is what i tried:

    try {
        stat = conn.createStatement();
    } catch (SQLException e) {
    }
    try {
        byte[] b = getFunction();
        stat.executeUpdate("update table set features="+b);
    } catch (SQLException e) {
    }

我得到以下错误:

java.sql.SQLException: unrecognized token: "[B@13a317a"

所以我想问题出在b?

最佳回答

使用PreparedStatement试试这个:

Connection con = null;
PreparedStatement stmt = null;

try {
    byte[] b = getFunction();
    con = ...;
    stmt = con.prepareStatement("update table set features=?");
    stmt.setBytes(1, b);
    stmt.executeUpdate();

    con.commit();
}
catch (SQLException e) {
    //handle exception (consider con.rollback()) and con maybe null here)
}
finally {
    //close stmt and at least con here (all maybe null here)
}

就我个人而言,我总是使用PreparedStatements。当您必须编写大量这样的代码时,可以考虑编写一些实用程序类来减少Boilerplate代码。

在处理纯JDBC时,尤其应该考虑为Connection、Statement和ResultSet方法上的null安全调用方法编写Utilty类。

EDIT What Thomas Jung wrote about preventing SQL Injections is another big pro for always using PreparedStatements. +1 for him :-)

问题回答

[B@13a317a看起来像是一个数组到字符串的结果(在本例中为b.toString())。您应该为blob使用一个准备好的语句,如:

update table set features=?

一个例子是此处

通常,您不应该通过串联字符串来创建SQL。这就是SQL注入问题的解决方案。

 stat.executeUpdate("update table set features="+b[0].toString()); 

你必须使用+





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签