What I did is, I created a new table in oracle database with image_id with numeric datatype and image_scr with blob. So, I wanted to insert image into the table from java.jframe with jdbc driver connected.
in below code I have defined photo as: byte[] photo=null; and filename as: String filename= null;
This is code for getting image from my computer
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();
}
}
});
This is database query code
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();
}
}
});