Java异常错误 - Sqlite preparedStatement.setBlob [英] Java Exception Error - Sqlite preparedStatement.setBlob

查看:1061
本文介绍了Java异常错误 - Sqlite preparedStatement.setBlob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据库和图像放入数据库,它可以是MYSQL数据库(服务器)或SQLITE数据库(平板电脑在路上)。 Java应用程序每天与服务器同步,上传新数据并下载任何新数据。这部分工作很好。

I'm placing and image into a database, it could be either an MYSQL database (the Server) or an SQLITE database (a Tablet PC for on the road). The Java application syncs with the server on a daily basis, uploading new data and downloading any new data. That part is working well. However, the requirement is for it to be able to handle images too.

而不是实现一个依赖于将镜像复制到每一端的文件系统的全新系统,我们选择在数据库中使用blob。不真正感兴趣的反应,说不做它;)我真正需要的帮助是,当我尝试写blob我得到一个异常在分派。

Rather than implement a whole new system that relies on copying the images to the filesystem on each end, we are opting to use blobs in the database. Not really interested in responses that say don't do it ;) what I really need help with is that when I try to write the blob I get an exception in the dispatching.

我们正在从数据库构建输入表单,因为整个应用程序正在用于不同的目的,具体取决于数据库。输入形式允许您将图像附加到记录,我们将其存储为base64字符串。然后将它解码成一个byte []。

We are constructing the input forms from the database, as the whole application is being used for different purposes, dependent on the database. The input form allows you to attach an image to the record, we store that as base64 string. Then decode it into a byte[].

我的测试程序在字符串和字节数组之间来回转换(最终一个Image)没有问题。所以我很相信问题是在预准备语句中设置Blob,但是我可能错了。

My test program converts back and forth between string and byte array (and ultimately an Image) no problem. So I'm fairly convinced that issue is in setting the Blob in the prepared statement, but I could be wrong.

点击保存按钮后发生异常。 / p>

The Exception occurs after the Save button is clicked.

Exception occurred during event dispatching:
java.lang.AbstractMethodError: org.sqlite.PrepStmt.setBlob(ILjava/io/InputStream;J)V
    at tabletapp.database.DB.prepareStatement(DB.java:641)
    at tabletapp.database.DB.save(DB.java:743)
    at tabletapp.FormPanel.saveData(FormPanel.java:546)

违规代码块

public void prepareStatement(String table, String sql, LinkedHashMap<String, String> data) {
    try {
        String typeID = "";
        PreparedStatement ps = connection.prepareStatement(sql);
        log.debug("Preparing SQL: " + sql.replace("\n", ""));
        int parameterIndex = 1;

        //for (String columnName : getColumnNames(table)) {
        for (String columnName : data.keySet()) {
            typeID = getColumnType(table, columnName);

            if (data.containsKey(columnName)) {
                String value = data.get(columnName);
                if (value == null || "".equals(value)) {
                    //log.debug(columnName+":"+parameterIndex+" set to null");
                    ps.setNull(parameterIndex, Types.NULL);
                } else {
                    //log.debug(columnName+":"+parameterIndex+" set to "+value);
                    switch (getColumnTypeId(table, columnName)) {
                        case Types.VARCHAR:
                        case Types.CHAR:
                            ps.setString(parameterIndex, value);
                            break;

                        case Types.TIMESTAMP:
                            DateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            java.util.Date timstamp = new java.util.Date();
                            ps.setString(parameterIndex, timestampFormat.format(timstamp));
                            break;

                        case Types.DATE:
                            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            java.util.Date date = new java.util.Date();
                            ps.setString(parameterIndex, dateFormat.format(date));
                            break;

                        case Types.SMALLINT:
                        case Types.INTEGER:
                        case Types.NUMERIC:
                            ps.setInt(parameterIndex, new Integer(value));
                            break;

                        case Types.FLOAT:
                            ps.setFloat(parameterIndex, new Float(value));
                            break;

                        case Types.BLOB:
                            // convert string to byte array to blob
                            byte[] bData = null;
                            try {
                               bData = new BASE64Decoder().decodeBuffer(value);
                               log.info("I have Bytes[]");
                            }
                            catch (Exception e){
                                log.info("Something went Horribly, Horribly Wrong");
                            }

                            // Note tried the follwowing
                            //Blob blob=connection.createBlob();
                            // blob.setBytes(bData.length, bData);
                            // ps.setBlob(parameterIndex,blob);

                            ByteArrayInputStream bais = new ByteArrayInputStream(bData);
                            ps.setBlob(parameterIndex, bais,bData.length);


                            break;

                        case Types.BOOLEAN:
                        case Types.BIT:
                            //log.debug(table + "." + columnName + " (boolean) = " + value);
                            if ("1".equals(value) || "true".equals(value)) {
                                ps.setBoolean(parameterIndex, true);
                            } else {
                                ps.setBoolean(parameterIndex, false);
                            }
                            break;
                    }
                }
                parameterIndex++;
            }
        }
        ps.executeUpdate();
        connection.commit();
    } catch (SQLException e) {
        log.error("Error in sql: " + sql);
        e.printStackTrace();
    }

}

p>

推荐答案

根据最近的帖子Xerial组方法setBlob未在sqlite-jdbc中实现。对我来说,建议的替代方案

According to a recent post Xerial group the method setBlob is not implemented in sqlite-jdbc. For me, the proposed alternative

preparedStatement.setBytes(idx,data)

工作正常,如果你的二进制数据足够小,加载到内存作为字节数组。

worked fine, if your binary data is small enough to load into memory as byte array.

这篇关于Java异常错误 - Sqlite preparedStatement.setBlob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆