Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:11:36+00:00 2026-06-03T07:11:36+00:00

I am using PostgreSQL as my database and java servlet to communicate with PostgreSQL.

  • 0

I am using PostgreSQL as my database and java servlet to communicate with PostgreSQL. My application requires that I fetch data from database for a matching input file. All the files are stored in the database in a bytea column. This is my code:

String checkdb="select * from key where certificate = '"+c+"';";
Statement stcheck=conn.createStatement(
    ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet newrs=stcheck.executeQuery(checkdb);
newrs.last();
int rowcount=newrs.getRow();
if(rowcount!=0)
throw new ArithmeticException();

where certificate is the name of the column containing the bytea data and c (uploaded) is a certificate in byte[] form. So here I am checking if the uploaded certificate is already present in the database. If it is present then I am throwing an exception.

But the problem is uploading the same certificate multiple times is not throwing an exception is working as if the two certificates are different.

Am I comparing the byte values correctly. What should I do to correct this?

EDIT
I tried setting the certificate column as the primary key. But still the certificate gets inserted into the database. How could this happen? The internal comparison also fails for some reason.
EDIT
I tried retrieving a ile and converted it to string :

InputStream input13 = retrs.getBinaryStream("certificate");
       ByteArrayOutputStream output13 = new ByteArrayOutputStream();
         byte buf[]=new byte[1024];
            int len;
    IOUtils.copy(input13, output13);
    input13.close();
    byte[] retvalue=output13.toByteArray();
    String s1=new String(retvalue);

String s2=new String(c);
System.out.println("in--------------"+s2);


System.out.println("out----------------"+s1);

retrs is the result set containing the just stored certificate. c is the input certificate in byte[]. The out put that I get is:

19:05:43,156 INFO  [STDOUT] in-------------------BEGIN CERTIFICATE-----
MIICIjCCAYsCAQMwDQYJKoZIhvcNAQEFBQAwYTELMAkGA1UEBhMCYXMxCzAJBgNV
BAgMAmFzMQswCQYDVQQHDAJhczELMAkGA1UECgwCYXMxCzAJBgNVBAsMAmFzMQsw
CQYDVQQDDAJjYTERMA8GCSqGSIb3DQEJARYCYXMwHhcNMTIwNDEwMDY0ODQ3WhcN
MTMwNDEwMDY0ODQ3WjBSMQswCQYDVQQGEwJhczELMAkGA1UECBMCYXMxCzAJBgNV
BAcTAmFzMQswCQYDVQQKEwJhczELMAkGA1UECxMCYXMxDzANBgNVBAMTBmNsaWVu
dDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAsfJq9TVgTLqz84nuWpDm+dvI
do2HIWoHkmEPNowK4xn1+qWUPCy30aASWI92YMtE6njtQGyZBYij0iTAxldDktqd
bMdpKAONDlSv71rn+fZPuyenbBFYZexybNVsRMFzTD/F/hiPPXT7E1j9CrgIMxVz
ScZeNATh/dPLwuNQ0dsCAwEAATANBgkqhkiG9w0BAQUFAAOBgQDDUyeIWlKaMMkM
8VRGuDJyKFC8miQfmul6zE4+6RTswHKjcUoje2vhTQgV+GBfqx8ai+ziujx8CeyR
eJWcb1G8gg+LMUNDcTOlXTeBG9L2c/gMOEu7BEjtpojSYmFyEvJEQ5Yep44HKZZb
FSPFVNxGiXBhNxtKQdAu6/smQ/eBNg==
-----END CERTIFICATE-----
19:05:43,156 INFO  [STDOUT] out----------------[B@17f8b39

Is my way of retrieving wrong. Because [B@17f8b39 looks like a memory location or something like that.
Answer
As said by @jarnbjo One thing has to be kept in mind. When ever you use byte array in postgressql whether for retieving or inserting do it only using the setBytes() .Else only the memory location(hash code in java) of the byte[] gets inserted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:11:38+00:00Added an answer on June 3, 2026 at 7:11 am

    Use a PreparedStatement:

    PreparedStatement pstat = conn.prepareStatement(
          "select * from key where certificate = ?");
    pstat.setBytes(1, c);
    ResultSet newrs = pstat.executeQuery();
    

    You should of course do the same when inserting the data as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jdbc to connect to a postgresql database in a java application
I'm using Hibernate and PostgreSQL 8.4 database in a Java application. I have the
I'm inserting text from a Java application into a Postgresql database, but it all
I am switching from using SQLite3 to PostgreSQL, and hoped that I could populate
I write an application based on an already existing database (postgreSQL) using JPA and
I am working on a Java implementation for temporal aggregation using a PostgreSQL database.
I have a stand-alone java application that makes some database read/write business on a
There's an enterprise application using Java + Hibernate + PostgreSQL. Hibernate is configured via
I working on Ruby on Rails application using a PostgreSQL database. I have heard
I'm loading a fixture generated from sqlite into postgresql database using manage.py loaddata. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.