
hi........
How to retrieve image from database when it is stored.
I have stored image in postgresql and want to retrieve in java form.
Can u tel me hw to do?
I am trying n able to do also but it retrieves as a string and not complete image can u tel me the conversion 4 n image.?

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class RetrieveImage{
public static void main(String[] args)throws Exception{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel();
JLabel lab2=new JLabel("Name");
JTextField text1=new JTextField(20);
JLabel lab3=new JLabel("Address");
JTextField text2=new JTextField(20);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "satya", "satya" );
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from test where id=1");
byte[] bytes = null;
if(rs.next()){
String name=rs.getString("name");
text1.setText(name);
String address=rs.getString("address");
text2.setText(address);
bytes = rs.getBytes("image");
Image image = f.getToolkit().createImage(bytes);
ImageIcon icon=new ImageIcon(image);
lab1.setIcon(icon);
}
lab1.setBounds(150,10,100,100);
lab2.setBounds(10,120,100,20);
text1.setBounds(150,120,100,20);
lab3.setBounds(10,150,100,20);
text2.setBounds(150,150,100,20);
f.add(lab1);
f.add(lab2);
f.add(text1);
f.add(lab3);
f.add(text2);
f.setVisible(true);
f.setSize(350,200);
}
}
For more information, Visit Here
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.