Java 需要在同一列中同时上载多个图像

Java 需要在同一列中同时上载多个图像,java,sql,Java,Sql,因此,如上所述,我需要上传更多的一个图像到我的数据库,我使用Jfilechooser来选择我的文件 我的代码一次只能处理一个图像,我有一个blob列来保存我的图像 我的sql查询的代码 try { PreparedStatement pst =null; ResultSet rst=(ResultSet) pst; Connection con=(Connection) DriverManager.getConnection("jdbc:

因此,如上所述,我需要上传更多的一个图像到我的数据库,我使用Jfilechooser来选择我的文件 我的代码一次只能处理一个图像,我有一个blob列来保存我的图像

我的sql查询的代码

try {

        PreparedStatement pst =null;
        ResultSet rst=(ResultSet) pst;

        Connection con=(Connection)    DriverManager.getConnection("jdbc:mysql://localhost/iqari", "root","");
        String sql="UPDATE first SET test = ? WHERE   id  = ?";
        pst=(PreparedStatement) con.prepareStatement(sql);
           int row = masterTable.getSelectedRow();

        Object d =   masterTable.getValueAt(row, 0);
        pst.setBytes(1,person_image);
        pst.setObject(2,d);
        pst.execute();
        JOptionPane.showMessageDialog(mainPanel, "done");
    } catch (Exception e) {


        System.out.println(e.getMessage());
    }
用于附加我的图像的代码 编辑


任何帮助都是有用的。

使用
.getSelectedFiles()
时出现错误的原因是它返回的是一个对象数组,而不是单个对象。试试这样的东西

更新的代码,我无法测试此

File[] files = chooser.getSelectedFiles();

// instantiates an array of byte arrays of size files.length
byte[][] images = new byte[files.length][];
int numberOfImages = 0;
for (File f : files) {
    filename = f.getAbsolutePath();
    try 
    {
        File image2 = new File(filename);
        FileInputStream fis = new FileInputStream(image2);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for(int readNum; (readNum = fis.read(buf)) != -1;){
           bos.write(buf, 0, readNum);
        }

        person_image = bos.toByteArray();

        // adds the byte array to your array & increases the numberOfImages
        images[numberOfImages++] = person_image;
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
在原始SQL代码中更改此部分

pst = (PreparedStatement) con.prepareStatement(sql);
int row = masterTable.getSelectedRow();

Object d = masterTable.getValueAt(row, 0);
pst.setBytes(1,person_image);
pst.setObject(2,d);
pst.execute();
对此,

for(byte[] imageAsByteArray : images) {
    pst = (PreparedStatement) con.prepareStatement(sql);

    int row = masterTable.getSelectedRow();
    Object d = masterTable.getValueAt(row, 0);

    pst.setBytes(1, imageAsByteArray);
    pst.setObject(2, d);
    pst.execute();
}

使用
.getSelectedFiles()
时出错的原因是它返回的是对象数组,而不是单个对象。试试这样的东西

更新的代码,我无法测试此

File[] files = chooser.getSelectedFiles();

// instantiates an array of byte arrays of size files.length
byte[][] images = new byte[files.length][];
int numberOfImages = 0;
for (File f : files) {
    filename = f.getAbsolutePath();
    try 
    {
        File image2 = new File(filename);
        FileInputStream fis = new FileInputStream(image2);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for(int readNum; (readNum = fis.read(buf)) != -1;){
           bos.write(buf, 0, readNum);
        }

        person_image = bos.toByteArray();

        // adds the byte array to your array & increases the numberOfImages
        images[numberOfImages++] = person_image;
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}
在原始SQL代码中更改此部分

pst = (PreparedStatement) con.prepareStatement(sql);
int row = masterTable.getSelectedRow();

Object d = masterTable.getValueAt(row, 0);
pst.setBytes(1,person_image);
pst.setObject(2,d);
pst.execute();
对此,

for(byte[] imageAsByteArray : images) {
    pst = (PreparedStatement) con.prepareStatement(sql);

    int row = masterTable.getSelectedRow();
    Object d = masterTable.getValueAt(row, 0);

    pst.setBytes(1, imageAsByteArray);
    pst.setObject(2, d);
    pst.execute();
}

你知道如何创建线程吗?不,很抱歉,我是新手,但是任何帮助都值得一试关于线程的教程。我想这就是你想要的:更多信息。可能重复的你知道如何创建线程吗?不抱歉,我是新手,但任何帮助都值得一试关于线程的教程。我想这就是你想要的:更多信息。可能是重复的,我现在就试试,请稍等。似乎我不能使用foreach,因为我正在使用netbeans jdk 6,它可能需要jdk 8抱歉,我的坏,只是一秒没有错误,但只保存一个图像,另一个没有显示在列中。你能用你正在使用的代码编辑你的原始帖子吗?我有种感觉,你只向你的桌面发送了一次
person\u image
,我现在就试试,请稍等。似乎我无法使用foreach,因为我正在使用netbeans jdk 6,它可能需要jdk 8抱歉,我的问题只是一秒没有错误,但只保存一张图像,另一张没有显示在专栏中。你能用它编辑你的原始帖子吗你正在使用的代码?我感觉你只给你的桌子发送了一次
person\u image