我正在使用Griaule的sdk指纹开发一个项目,作为一个开始我创建一个没有GUI的程序,允许用户扫描他的指纹并将其存储在之前创建的mysql数据库中。 我在这里向您询问有关将指纹存储在数据库中的问题。 在我创建的程序中,我捕获了指纹,我通过一个名为extract()的函数从指纹中提取模板。 之后我应该调用另一个函数enroll(),它允许我将指纹保存在数据库中。 即使查看SDK示例我也不明白它是如何工作的,有人可以帮助我吗?提前致谢! :)
public void enroll() {
try {
//Inserts the template on the database
enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);
enrollStmt.executeUpdate();
//Picks the ID generated for it.
ResultSet rs = insertedIdStmt.executeQuery();
rs.next();
ui.writeLog("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));
} catch (SQLException e) {
ui.writeLog("Error enrolling template");
}
}
答案 0 :(得分:1)
好的,非常感谢Hirak,所以我用我创建的一个名为initdb()的函数打开一个新连接,结构如下:
private void initDB() {
try {
//Loads the JDBC driver.
Class.forName("com.mysql.jdbc.Driver").newInstance();
/**Connection to the Database.*/
Connection db;
String user = "root";
String password = "";
// connect to a memory database
db = DriverManager.getConnection("jdbc:mysql://localhost:3306/impronte?user=" + user + "&password=" + password);
Statement stm = (Statement) db.createStatement();
//Creates the statements that will be executed on the database,
enrollStmt = db.prepareStatement("INSERT INTO persona(template) values(?)");
insertedIdStmt = db.prepareStatement("SELECT MAX(ID) FROM persona");
} catch (Exception e) {
System.out.println("Error connecting to the database.");
System.out.println(e.getMessage());
}
}
并在注册函数内部使用此代码,但仍然给出了错误(“错误注册模板”):
public void enroll ( Template template ) throws GrFingerJavaException {
try {
//Inserts the template on the database
enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);
enrollStmt.executeUpdate();
//Picks the ID generated for it.
ResultSet rs = insertedIdStmt.executeQuery();
rs.next();
System.out.println("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));
System.out.println("Fingerprint enrolled");
} catch (SQLException e) {
System.out.println("Error enrolling template");
}
}
答案 1 :(得分:1)
你需要在调用binaryStream之前将template
设置为你正在使用的sdk的模板类,看看我的意思:第6行如果我正确的话。
public void enroll ( Template template ) throws GrFingerJavaException {
try {
//Inserts the template on the database
Template temp = template;
if(temp != null){
byte[] b = temp.serialize();
enrollStmt.setBytes(1, b);
}
enrollStmt.executeUpdate();
//Picks the ID generated for it.
ResultSet rs = insertedIdStmt.executeQuery();
rs.next();
System.out.println("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));
System.out.println("Fingerprint enrolled");
} catch (SQLException e) {
System.out.println("Error enrolling template");
}
}
答案 2 :(得分:0)
将指纹数据保存为数据库中的BLOB。 Blob(二进制大对象)不过是信息的字节数组表示,主要用于在数据库中存储图像等。在您的情况下,指纹信息正在存储。
enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);
在此行中,使用模板对象中的数据创建bytearrayinputstream。 template.getData为您提供指纹信息的byte []表示。然后字节[]通过
保存在数据库中enrollStmt.executeUpdate();
然而,以下查询为您提供了存储数据的ID,供您使用。
ResultSet rs = insertedIdStmt.executeQuery();