2016-11-30 43 views
0

我有一个Java Swing应用程序和SQLite数据库的小问题。每当我尝试使用更新按钮更新表格中的选定行时,我的应用程序就会将所选行中存储的图像从数据库中删除。Java swing&sqlite - 更新图片

我删除功能:

String sql = "UPDATE PLAYER SET NAME =? , LAST=?, DOB=?, IMAGE=?, GENDER=?, LAST_UPDATE=? WHERE ID_PLAYER=? "; 

Timestamp timestamp = new Timestamp(System.currentTimeMillis()); 
int id = Integer.parseInt(tf_id.getText()); 

try { 
    pst = conn.prepareStatement(sql); 
    pst.setString(1, tf_name.getText()); 
    pst.setString(2, tf_last.getText()); 

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); 
    String d = sdf.format(t_date.getDate()); 
    pst.setString(3, d); 

    pst.setBytes(4, person_image); 

    if (combobox_male.isSelected()) { 
     gender = "MAN"; 
    } else if (combobox_female.isSelected()) { 
     gender = "WOMAN"; 
    } 
    pst.setString(5, gender); 

    pst.setTimestamp(6, timestamp); 

    pst.setInt(7, id); 
    pst.executeUpdate(); 
} catch (Exception e) { 
    JOptionPane.showMessageDialog(null, e); 
} 

我附加图片功能:

JFileChooser jfc = new JFileChooser(); 
jfc.showOpenDialog(null); 
File f = jfc.getSelectedFile(); 
filename = f.getAbsolutePath(); 
tetxfield_attach_image.setText(filename); 
lable_picture.setIcon(ResizeImage(filename, null)); 
try { 
    File image = new File(filename); 
    FileImageInputStream fis = new FileImageInputStream(image); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    byte[] buff = new byte[1024]; 
    for (int readNum; (readNum = fis.read(buff)) != -1;) { 
     bos.write(buff, 0, readNum); 
    } 
    person_image = bos.toByteArray(); 
} catch (Exception e) { 
    JOptionPane.showMessageDialog(null, e); 
} 

我在哪里错了?

+1

对不起,但不可能理解这一点:“我的应用程序,存储的数据库中的选定行的数据库删除图像。” – Berger

+1

你的问题是什么?更新查询删除行? – AxelH

+1

对实际问题的重要澄清将会很有用。 –

回答

0

检查在将person_image设置为pst之前是否正确启动了person_image?你提到没有新的数据,所以它似乎是唯一的机会丢失图像字节。

+0

Thx世界的帮助,我的问题解决了一些额外的代码。我在更新按钮事件中添加了单独的图像代码。 1.选择仅用于图像的语句; 2.然后选择位于标签图像中的图像; 3.只为图像更新声明(+选定玩家ID)。最好的问候,Caks – Caks