我在Access数据库中存储所有数组字节时遇到问题。
KeyPair pair = Certificate.generateKeyPair();
X509Certificate cert = Certificate.generateCertForCAroot(pair);
byte [] bytes = cert.getEncoded();
X509Certificate cert2 = Utils.Certificate.recreateCertFromBytes(bytes);
byte [] bytes2 = cert2.getEncoded();
if(cert2.equals(cert))System.out.println("0K**************:)");
else System.err.println("KO000#########################:(");
AccessBD bd = new AccessBD("C:\\Users\\khaled\\Desktop\\BDtest.accdb","","");
bd.connexion();
Connection con = bd.getCon();
String sql ="insert into Client(nom,certificat)values(?,?)";
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setObject(1, "CAroot");
pstmt.setObject(2, bytes);
pstmt.execute();
pstmt.close();
sql="select * from Client where nom='CAroot'";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs =ps.executeQuery();
rs.next();
String s = rs.getString("nom");
System.out.println(s);
//rs.next();
byte [] bytes3 = rs.getBytes("certificat");
ps.close();
System.out.println("length bytes: "+bytes.length+" length bytes2: "
+bytes2.length+ " length bytes3: "+bytes3.length);
bd.deconnexion();
输出:
0K**************:)
connexion a la base OK
CAroot
length bytes: 498 length bytes2: 498 length bytes3: 249
deconnexion a la base OK
BUILD SUCCESSFUL (total time: 2 seconds)
答案 0 :(得分:0)
尝试按this post使用PerparedStatement.setBinaryStream()
方法。另外,请确保您在Access中的数据类型等同于BLOB
答案 1 :(得分:0)
显而易见的是,您获得的数据是您预期的一半,这表明字符编码存在问题。如果我记得,可以将访问字符串字段设置为自动打包unicode数据,这样就可以参与其中。