我将密钥对保存到数据库中,以字节为单位:
byte[] pubkey = pair.getPublic().getEncoded();
byte[] privkey = pair.getPrivate().getEncoded();
String sql = "INSERT INTO certs(name, pubkey, privkey) VALUES(?,?,?)";
try (Connection conn = connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setBytes(2, pubkey);
pstmt.setBytes(3, privkey);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
这很有效。但是,当我想从数据库中获取密钥时,它不会。
public static byte[] getPubKey(String name){
String sql = "SELECT pubkey FROM certs WHERE name = ?";
byte[] result = null;
try (Connection conn = connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
result = rs.getBytes(1);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return result;
}
它返回: ResultSet已关闭 空
我该如何解决这个问题?如何将密钥格式化为普通密钥格式?
答案 0 :(得分:0)
SELECT查询需要一个参数。