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 5936697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:26:07+00:00 2026-05-22T15:26:07+00:00

This code takes picture test.gif and puts the picture bit value into a bytea

  • 0

This code takes picture test.gif and puts the picture bit value into a bytea coloumn.

public void addImage() throws SQLException, IOException {
    Connection con = openConnection();

    File file = new File("test.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = con.prepareStatement("INSERT INTO image VALUES (?, ?, ?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int)file.length());
    ps.setInt(3,1);
    ps.executeUpdate();
    fis.close();

}

My next goal is to display the picture that is saved in the database in bytes. How can this be done ?

  • 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-05-22T15:26:08+00:00Added an answer on May 22, 2026 at 3:26 pm

    Something like:

    // Run a query again the IMAGE table to fetch the image with the given id
    public Image readImage(Connection conn, int id) throws SQLException {
      PreparedStatement pstmt = null;
      try {
        pstmt = conn.prepareStatement("SELECT contents FROM image WHERE id = ?");
        pstmt.setInt(1, id); 
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
          InputStream is = rs.getBinaryStream(1);
          return ImageIO.read(is);
        } else {
          return null;
        }
      } finally {
        if (pstmt != null) {
          try {
            pstmt.close();
          } catch (SQLException ignored) {
          }
        }
      }
    }
    
    // Display the given Image on a Swing frame
    public void showImage(final Image img) {
      JFrame frame = new JFrame("Image");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(img.getWidth(), img.getHeight());
      frame.add(new JPanel() {
        public void paint(Graphics g) {
          g.drawImage(img, 0, 0, null);
        }
      });
      frame.show();
    }
    

    might work for you.

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

Sidebar

Related Questions

Take a look at this code: public class Test { public static void main(String...
In my computer this code takes 17 seconds (1000 millions times): static void Main(string[]
So I have this code that takes care of command acknowledgment from remote computers,
Problem Hello all! I have this code which takes my jpg image loops through
i found this code at activestate, it takes a string and prints permutations of
Take this code into consideration: Proxy p = new Proxy(Type.SOCKS, new InetSocketAddress(proxyURL, port)); try
I am building a picture gallery, that uses this code to display each product
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
I use this code which is taken from MVC futures and attach the Attribute

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.