I am using JdbcTemplate from Spring framework.
The database is Postgres.
Issue is I am reading .cer file in java and want to store the content of this certificate to postgress database of column type OID and using jdbctemplate but getting exception is
column "cert_file" is of type oid but expression is of type bytea
Below is code
final String sql1 = "INSERT INTO sp_certificate_detail " +
"(public_key,cert_file)values(?,?)";
status = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(
sql1, new String[] { "cert_id" });
ps.setString(1, spreg.getPublicKey());
ps.setBytes(2, spreg.getCertFileContent());//This is byte[] of type
return ps;
}
}, keyHolder);
To get certiticate content am using below code
cfb = CertificateFactory.getInstance("X.509");
X509Certificate certb = (X509Certificate) cfb.generateCertificate(fileInputStream);
spreg.setCertFileContent(certb.getEncoded());
Please anyone suggest the solution
I have tried converting byte[] to LOB then getting different exception
Large Objects may not be used in auto-commit mode.; nested exception is org.postgresql.util.PSQLException: Large Objects may not be use d in auto-commit mode.
Code is below
ps.setBlob(10, (new SerialBlob(spreg.getCertFileContent())));
byteaas the data type of the column instead? It's a much better choice for a "BLOB" column - especially when using JDBC.byteacolumn can be accessed through JDBC's BLOB API the same way you would access aBLOBcolumn in Oracle or avarbinarycolumn in SQL Server. No Java framework that I know of supports Postgres' large objects, but most of them supportbytea.