iamjc015
iamjc015

Reputation: 2237

Inserting values into BLOB type in JDBC

I have several columns of BLOB type in my Database. Not all these columns are needed to be filled since it will contain fingerprint templates. There are cases that users are amputated thus, not all columns are used.

In that situation, what value should I insert in my database? This is my code:

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();
    }

Thanks!

Upvotes: 0

Views: 1112

Answers (1)

Raffaele
Raffaele

Reputation: 20885

You just need to insert NULL in the database, so a simple check is enough before calling methods that throw exceptions in the Java program. For example for left hand thumb:

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);
}

To avoid duplicate code (5 times for two hands) I suggest you put this inside your Fingerprint class (or whatever is called).

Upvotes: 1

Related Questions