2016-11-30 42 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