java - 在 JDBC 中将值插入 BLOB 类型

标签 java mysql jdbc

我的数据库中有几列 BLOB 类型。 并非所有这些列都需要填写,因为它将包含指纹模板。存在用户被截断的情况,因此并非所有列都被使用。

在这种情况下,我应该在数据库中插入什么值?这是我的代码:

public void insertLeftHandBio(Bio bio, String id) {
        try {

            String query = "INSERT INTO fingerprint_left_tbl VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";

            ps = super.getConnection().prepareStatement(query);

            ps.setInt(1, 0);
            ps.setString(2, id);

            //LEFT HAND
            //Left Thumb
            File file = new File("Left Hand Thumb.jpg");
            InputStream stream = (InputStream) new FileInputStream(file);
            ps.setBytes(3, bio.getLeftHandThumbTemplate().serialize());
            ps.setBinaryStream(4, stream);

            //Left Index
            file = new File("Left Hand Index.jpg");
            stream = (InputStream) new FileInputStream(file);
            ps.setBytes(5, bio.getLeftHandIndexTemplate().serialize());
            ps.setBinaryStream(6, stream);

            //Left Middle
            file = new File("Left Hand Middle.jpg");
            stream = (InputStream) new FileInputStream(file);
            ps.setBytes(7, bio.getLeftHandMiddleTemplate().serialize());
            ps.setBinaryStream(8, stream);

            //Left Ring
            file = new File("Left Hand Ring.jpg");
            stream = (InputStream) new FileInputStream(file);
            ps.setBytes(9, bio.getLeftHandRingTemplate().serialize());
            ps.setBinaryStream(10, stream);

            //Left Little
            file = new File("Left Hand Little.jpg");
            stream = (InputStream) new FileInputStream(file);
            ps.setBytes(11, bio.getLeftHandLittleTemplate().serialize());
            ps.setBinaryStream(12, stream);

            int i = ps.executeUpdate();

            if (i < 0) {
                System.out.println("Bio Inserted");
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        super.cleanUpResources();
    }

谢谢!

最佳答案

你只需要在数据库中插入NULL,因此在调用Java程序中抛出异常的方法之前进行简单的检查就足够了。以左手拇指为例:

if (bio.getLefHandThumbTemplate() != null) {
  File file = new File("Left Hand Thumb.jpg");
  InputStream stream = new FileInputStream(file);
  ps.setBytes(3, bio.getLeftHandThumbTemplate().serialize());
  ps.setBinaryStream(4, stream);
} else {
  ps.setNull(3, Types.BLOB);
  ps.setNull(4, Types.BLOB);
}

为了避免重复代码(两只手 5 次),我建议您将其放入 Fingerprint 类(或任何名称)中。

关于java - 在 JDBC 中将值插入 BLOB 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304779/

相关文章:

java - Android 和 Java key 生成返回不同的结果

java - 如何在 JBoss 中配置 SQL Server 数据源以使用特定的 Active Directory 用户进行连接?

java - 如何将 Apache Tomcat 5 中运行的 Java 应用程序迁移到 Apache Tomcat 7?

java - 用点符号命名的 JPA 实体

java - 如何在 Linux、Mac 和 Windows 上从 Java 启动 .NET 应用程序?

jdbc - 为什么数据库的编程接口(interface)称为驱动程序?

java - 使用 JDBC 访问 Hive 元数据

java - 如何为 SQL 查询转义字符串(没有准备好的语句)

c# - 使用 C# 在 Mysql 中使用回滚

php - PHP下的mysql_query是阻塞函数吗?