2

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();
2
  • are you sure that your query is well-formed? These brackets seems to be illegal... Commented Jul 2, 2012 at 13:03
  • @AlexStybaev: the brackets are Microsoft's deviation from the SQL standard. Commented Jan 4, 2014 at 10:30

2 Answers 2

3

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){
      //
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay so how would I use the UPDATE statement for the bytes? Sorry, I'm VERY new to SQL
Thankyou! This was amazingly helpful.
2

You should do something like this:

Connection con = getConnection();
PreparedStatement ps = null;
con = getConnection();
ps = con.prepareStatement("insert into tableName (column, blobColumn...) values (?, ?)");
ps.setString(1, "ID");
File image = new File("C:/yourPath.../...");
inputStream = new FileInputStream(image);
ps.setBinaryStream(2, (InputStream) inputStream, (int) (image.length()));
ps.executeUpdate();

That's it, now you inserted your img to DB.

Comments

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.