简体   繁体   中英

how can I set an image in to seventh column?

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();
}

}

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.)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM