我的数据库中有几列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();
}
谢谢!
答案 0 :(得分:1)
您只需要在数据库中插入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
类(或任何被称为的)中。