0

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())));
3
  • Why don't you use bytea as the data type of the column instead? It's a much better choice for a "BLOB" column - especially when using JDBC. Commented Nov 14, 2014 at 10:43
  • I asked my DBA he is saying if we port to other databases OID is better choice than bytea,so that is reason he created columns type as OID Commented Nov 14, 2014 at 10:45
  • Your DBA is wrong. "Large Objects" (ala "oid") require a completely different programming model to read and write. bytea column can be accessed through JDBC's BLOB API the same way you would access a BLOB column in Oracle or a varbinary column in SQL Server. No Java framework that I know of supports Postgres' large objects, but most of them support bytea. Commented Nov 14, 2014 at 10:46

2 Answers 2

1

Integrate below piece of code with your code, should work.

import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;

(package available in postgresql jdbc jar)

LargeObjectManager largeObjectManager = null;

LargeObject largeObject = null;

Long largeObjectRef = null;

largeObjectManager = ((org.postgresql.PGConnection) connection).getLargeObjectAPI();

largeObjectRef = largeObjectManager.createLO(LargeObjectManager.READ |    LargeObjectManager.WRITE);

largeObject = largeObjectManager.open(largeObjectRef, LargeObjectManager.WRITE);

largeObject.write("string/file content to write into oid column".getBytes());

largeObject.close();

include your rest of code / parameter which needs to be set and along with below,

ps.setLong(2, largeObjectRef);

ps.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

0

I have changed column type to bytea now its working

1 Comment

May i ask how did you change oid type to bytea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.