我正在开发我的第一个Java应用程序,并且一直在尝试实现我能够实现的所有功能。以下是我的问题:
我已经实现了jTable。系统将数据从数据库加载到jTable。当我单击jTable中的一行或使用我的箭头键时,我能够将所选行中的数据加载到相应的字段,编辑和更新数据。但是,当我用新的图像替换现有图像时,我无法更新我的数据库。
假设我还在数据库中为音频和视频文件创建了列。如何将它们上传到我的数据库,进行更改并使用更改更新数据库?我可以用文字来做,但我的困难在于图像,音频和视频文件。 希望像往常一样接受你的杰出贡献。
以下是我的代码:
我将以下代码附加到“保存”按钮:
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String sql ="Insert into Counsellee (counselleeID,firstName,surname,address,telNo,gender,counsellorID,date,photo) values (?,?,?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
pst.setString(1, counselleeIDField.getText());
pst.setString(2, firstNameField.getText());
pst.setString(3, surnameField.getText());
pst.setString(4, addressField.getText());
pst.setString(5, TelField.getText());
String value=genderComboBox.getSelectedItem().toString();
pst.setString(6,value);
pst.setString(7, counsellorIDField.getText());
pst.setString(8, ((JTextField)transdateChooser.getDateEditor().getUiComponent()).getText());
pst.setBytes(9, person_image);
pst.execute();
/*
* To clear input fields for the next entry
*/
counselleeIDField.setText("");
firstNameField.setText("");
surnameField.setText("");
addressField.setText("");
TelField.setText("");
genderComboBox.setSelectedItem("");
counsellorIDField.setText("");
/*I want to return the date field to the current date and clear the image selection path for the next image, but I
* have no idea how to go about it.
*/
JOptionPane.showMessageDialog(null, "Saved");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
Update_table();
try {
pst.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
我将以下代码附加到“更新”按钮:
btnUpdateCounsellee.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String value1= counselleeIDField.getText();
String value2= firstNameField.getText();
String value3= surnameField.getText();
String value4= addressField.getText();
String value5= TelField.getText();
String value6=genderComboBox.getSelectedItem().toString();
String value7 =counsellorIDField.getText();
java.util.Date value8 = transdateChooser.getDate();
byte[] value9 = person_image.toString().getBytes(); //something seems wrong with this line as it is not converting my image as BLOB.
//This is where my problem is.
String sql="update Counsellee set counselleeID='"+value1+"' ,firstName ='"+value2+"',surname ='"+value3+"',address='"+value4+"',telNo ='"+value5+"',gender ='"+value6+"',counsellorID ='"+value7+"',date ='"+value8+"',photo ='"+value9+"' where counselleeID='"+value1+"' ";
pst=conn.prepareStatement(sql);
pst.execute();
/*
* clear input fields for next action
*/
counselleeIDField.setText("");
firstNameField.setText("");
surnameField.setText("");
addressField.setText("");
TelField.setText("");
genderComboBox.setSelectedItem("");
//I want to set date to current date here again
JOptionPane.showMessageDialog(null, "Updated");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
try {
pst.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
当我点击更新时,系统实际上将所有内容上传到数据库,包括图像。但是图像没有保存为BLOB,所以我无法查看它。