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

  • Home
  • SEARCH
  • 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 8174191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:23:18+00:00 2026-06-06T22:23:18+00:00

What I need to do (And I know it’s bad practice, but I’m kind

  • 0

What I need to do (And I know it’s bad practice, but I’m kind of forced to) is upload an image to an SQL DB from Java. Currently, I’m using a prepared statement and trying to use this to upload the bytes from the image.

public void insertImage(Connection conn,String img,String strItemNum)
{

        String query;
        PreparedStatement pstmt;

        try
        {
                File file = new File(img);
                FileInputStream fis = new FileInputStream(file);
                byte[] image = new byte[(int) file.length()];
                fis.read(image);
                System.out.println("image as sent " + image.length);
                query = ("SELECT [Item Picture] from [Inventory] where [Item Number] = '" + strItemNum + "'");
                pstmt = conn.prepareStatement(query);
                System.out.println(pstmt.getMetaData().getColumnName(1) + " of type: " + pstmt.getMetaData().getColumnTypeName(1));
                pstmt.setBytes(1,image);
                pstmt.executeUpdate();
                pstmt.close();


        }
        catch (IOException | SQLException e)
        {
            System.err.println(";"+e.getMessage());
            e.printStackTrace();

        }

}

But this is yielding an SQLException: Invalid parameter index 1. The parameter in my table is of type “image” and I can’t get anything to stick. I’ve tried using .setBlob but from my google research, it appears that Blobs were never implemented very well.

EDIT: Solved by using AVD’s answer

changed to

query = ("Update  [Inventory] set [Item Picture] = ? where [Item Number] = ?");
pstmt = conn.prepareStatement(query);
pstmt.setBytes(1,image);
pstmt.setString(2, strItemNum);
pstmt.executeUpdate();
pstmt.close();
  • 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-06T22:23:19+00:00Added an answer on June 6, 2026 at 10:23 pm

    You should have to use INSERT or UPDATE sql statement to add a new record or update an existing row.

    String sql="INSERT INTO TableName (Col1,Col2) VALUES (?,?)";
    

    Use ? (question mark) to specify the placeholder.

    EDIT:

    Connection connection=null;
    PreparedStatement statement=null;
    String sql="UPDATE TableName set ImageCol1=? WHERE ID=?";
    try{
      //Obtain connection 
      statement=connection.prepareStatement(sql);
      statement.setBytes(1,byteArray);
      statement.setInt(2,10);
      ...
    }catch(SQLException ex){
      //
    }finally{
      if(ps!=null) {
        try{
          ps.close();
        }catch(Exception ex){
          //
        }
      }
     if(cn!=null) {
        try{
          cn.close();
        }catch(Exception ex){
          //
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know if any kind of file can be signed digitally, using
I need to know how to exclude files/folders from php generated zip. Here is
I need to know which iOS device is currently running app (saying more exactly
I need to know which gtktreeview was passed to a function from the handler
I need know what elements are in current mouse position and I'm using jQuery.
I want to make a list view of names. But I need know how
Need to know if I need to worry about people using this on my
I need to know that how to read the directory path from a .properties
I need to know how to extract raw voice data from android real time.
I need know how can get a parameter from the URL. I mean if

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.