tags:

views:

75

answers:

3

hi, I have a sql table which has 7 column and the first six column ,store string type but the last one store BLOB type.I get those 6 String types with the addBirth method and for storing the image for last column ,I use insertImageToBirthTable method. I want to store Strings and BLOB not together because the user may not chose a photo for adding a person.(I have edited my post and now I use this method for storing my image but it still have null value in the SQL table,why???)Also I have printed pathfile and it wasn't null.

my insertImageToBirthTable method:

public static void insertImageToBirthTable(String name, String family, String fatherName, String motherName, String dateOfBirth, String placeOfBirth, String pathFile) {

try {
    System.out.println(pathFile);
    File file = new File(pathFile);
    FileInputStream input = new FileInputStream(file);
    PreparedStatement stmt = (PreparedStatement) conn.prepareStatement("UPDATE birthtable SET image =? WHERE name = '" + name + "'AND family ='" + family + "'AND fatherName = '" + fatherName + "'AND motherName ='" + motherName + "' AND dateOfBirth = '" + dateOfBirth + "' AND placeOfBirth = '" + placeOfBirth + "'");
    stmt.setBinaryStream(1, input);
    stmt.executeUpdate();


} catch (Exception e) {
    e.printStackTrace();
}

}

+3  A: 

Reading the message of the exception reveals:

No value specified for parameter 1

So… do I need to spell it out for you? (Hint: you need to specify values for all parameters in your PreparedStatement. A single value will not do.)

Bombe
I get it!thanks a lot [:|]
Johanna
+1  A: 

If you want to add the image later, then you need a "primary key" in your table. The primary key gives you a simple way to identify a specific column. Add a column like this to your table:

ID serial,

Omit this column during insert and run this query after the insert to learn the primary key of your new column:

SELECT @@IDENTITY

Later, when you finally get the image, run this update:

update set imageBlob = ? where ID = ?

Use your InputStream for the first parameter and the primary key for the second.

[EDIT] See this page for a complete example how to insert an image into a table.

Aaron Digulla
thanks,I will try it.
Johanna
this query is true? if I want to use name instead of id.UPDATE birthtable SET image = '"+input+"' WHERE name = '"+name+"';
Johanna
This is the string to pass to `prepareStatement()`. then you need to set the parameters. **Never** use string concatenation to add parameters to your SQL statements.
Aaron Digulla
I get it ,thanks.and I use preparedStatement() too,but the value is null in SQL table!!!
Johanna
Which value is null?
Aaron Digulla
A: 

You are getting the exception because you need to provide all the parameters that you have in your sql (all the question marks).

If you only need to provide the imageBlob (I'm guessing that was you're calling it) column while defaulting all the other columns you can name the columns that you want to specify values for in your sql, try this,

query = ("insert into birthtable (imageBlob) VALUES (?)");

Although an update sounds more appropriate here than an insert, another option is to include it in your or main insert and supply null for this column when there is no image.

Mike Nelson