如何使用Java将图像、视频和音频文件上载到数据库,并替换它们和更新数据库?
我正在开发我的第一个Java应用程序,并且一直在尝试实现我能实现的所有可能的功能。以下是我的问题: 我已经实现了jTable。系统将数据从数据库加载到jTable。当我单击jTable中的一行或使用箭头键时,我能够将所选行中的数据加载到相应的字段,编辑和更新数据。但是,当我用新图像替换现有图像时,我无法更新数据库 假设我还在数据库中为音频和视频文件创建列。如何将它们上载到数据库、进行更改并使用更改更新数据库?我可以用文本来做,但我的困难是图像、音频和视频文件。 希望一如既往地收到您的精彩贡献 以下是我的代码: 我在“保存”按钮上附加了以下代码:如何使用Java将图像、视频和音频文件上载到数据库,并替换它们和更新数据库?,java,image,audio,video,Java,Image,Audio,Video,我正在开发我的第一个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,因此我无法查看它。您使用的是什么数据库?你能确认DB表的结构吗?对于blob,通常的情况是需要插入行,然后获取到该列的流,并将数据流到其中。@Crollster我使用的是SQLite