1

I'm trying upload image to database using Java.
I've used following code to do that work.

File file= new File("image.jpg");
FileInputStream fis = new FileInputStream("image.jpg");

String query = "insert into mytable(id,image) values(?, ?)";
PreparedStatement stmt = dbConn.prepareStatement(query);
stmt.setInt(1, sid);
stmt.setBinaryStream(2, fis, (int) file.length());

stmt.executeUpdate(); 

But it throws me this error.

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

Please help me to solve this problem.

6
  • are you sure the exception is at image part? or id? Commented Sep 7, 2011 at 6:05
  • I can't find where the error is. As I understood, it says there aren't any parameters. But as I saw in Java help, this is the way to do it. Commented Sep 7, 2011 at 6:19
  • Can you show full stacktrace? Commented Sep 7, 2011 at 7:33
  • @mgamulin This is a problem which one of my friend asked me. This is only information I have now. Will try to get that info from him. Thanks for concern.. :) What I can't understand is, examples which I found in web and according to Java help, this is the correct way to do it. Why doesn't identify parameters? Commented Sep 9, 2011 at 5:08
  • 2
    Can you set breakpoint on line after File file = new File("image.jpg"), and see what is file.length. Are you sure that your image path is valid, I'm not to sure that it is. Commented Sep 9, 2011 at 7:33

1 Answer 1

1

Feels like you have an error at "FileInputStream fis = new FileInputStream(fin);" may be i,m wrong, but what is 'fin'?. you should give that argument to be 'file'. Try doing

FileInputStream fis = new FileInputStream(file);

This might work.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, its my mistake. File name is variable in my code. I removed it for here. But I forgot to remove all. I edited question. Thanks for mention it.

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.