【发布时间】:2018-03-29 11:56:33
【问题描述】:
我所做的是,我在 oracle 数据库中创建了一个带有数字数据类型的 image_id 和带有 blob 的 image_scr 的新表。 所以,我想在连接了 jdbc 驱动程序的情况下从 java.jframe 将图像插入到表中。
在下面的代码中,我将照片定义为: 字节[] 照片=空; 和文件名为:String filename= null;
这是从我的计算机获取图像的代码
JButton btnNewButton = new JButton("Change picture");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
lblNewLabel_2.setIcon(new ImageIcon(f.toString()));
filename = f.getAbsolutePath();
textField_5.setText(filename);
try {
File image = new File(filename);
FileInputStream fis = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum =fis.read(buf))!=-1;)
{
bos.write(buf,0,readNum);
}
photo= bos.toByteArray();
fis.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
});
这是数据库查询代码
JButton btnNewButton_2 = new JButton("insert");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String query ="insert into stu_images(student_id,stu_image)values(01,'"+photo+"')";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.executeQuery();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
});
【问题讨论】: