如何将图像设置到第七列?

发布于 2024-08-15 02:34:48 字数 1046 浏览 5 评论 0原文

我有一个 sql 表,有 7 列和前六列,存储字符串类型,但最后一个存储 BLOB 类型。我使用 addBirth 方法获取这 6 个字符串类型,并且为了存储最后一列的图像,我使用 insertImageToBirthTable 方法。 我不想将字符串和 BLOB 存储在一起,因为用户可能不会选择照片来添加人物。(我已经编辑了我的帖子,现在我使用此方法来存储我的图像,但它在 SQL 表中仍然有空值,为什么???)我还打印了路径文件,它不为空。

我的 insertImageToBirthTable 方法:

public static void insertImageToBirthTable(String name, String family, StringfatherName, 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();
}

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡莣 2024-08-22 02:34:48

阅读异常消息可以发现:

没有为参数 1 指定值

那么...需要我为您拼写出来吗? (提示:您需要为 PreparedStatement 中的所有参数指定值。单个值是不行的。)

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

断舍离 2024-08-22 02:34:48

如果您想稍后添加图像,那么您的表中需要一个“主键”。主键为您提供了一种识别特定列的简单方法。将这样的列添加到您的表中:

ID serial,

在插入期间省略此列,并在插入后运行此查询以了解新列的主键:

SELECT @@IDENTITY

稍后,当您最终获得图像时,运行此更新:

update set imageBlob = ? where ID = ?

使用您的 InputStream 为第一个参数,主键为第二个参数。

[编辑] 有关完整示例,请参阅此页面如何将图像插入到表格中。

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.

烟沫凡尘 2024-08-22 02:34:48

您收到异常是因为您需要提供 sql 中的所有参数(所有问号)。

如果您只需要提供 imageBlob (我猜这就是您所说的)列,同时默认所有其他列,您可以命名要在 sql 中指定值的列,请尝试此操作,

query = ("insert intobirthtable (imageBlob) VALUES (?)");

虽然在这里更新听起来比插入更合适,但另一种选择是将其包含在您的或主插入中,并在以下情况下为此列提供 null没有图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文