How to store an image into MySQL using JDBC and display the image in JSP?

Recommended Answers

All 5 Replies

What exactly you trying to do?

I have designed a web page in which some images should be displayed.I have inserted the image using blob in MySQl database.. Now i have to retrieve the image and display it in a JSP page..It is displaying some error like

sun.awt.image.ToolkitImage@67f797...

Can u guide me?

Please post the code you using to retrieve the image and exact error

Thanks for ur immediate reply..

The coding is as follows:

For Inserting the images

String id=request.getParameter("bid");
String bname=request.getParameter("bname");
String s=request.getParameter("price");
Number num=NumberFormat.getInstance().parse(s);
int price=num.intValue();
Class.forName("com.mysql.jdbc.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:mysql://server17/blog","root","root");
FileInputStream is = new FileInputStream("C:/Documents and Settings/CEAdmin.SERVER17/Desktop/image/ca.gif");


 PreparedStatement ps=conn.prepareStatement("insert into book(bid,bname,amt,pict) values(?,?,?,?);");

ps.setString(1,id);
ps.setString(2,bname);
ps.setInt(3,price);
ps.setBinaryStream(4,is,len);
ps.executeUpdate();

For retrieving the images:

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://server17/blog","root","root");
Statement state=con.createStatement();
			
			ResultSet rs3= state.executeQuery("select pict from book where bid='345'");
			if(rs3.next()){
			    byte[] b=new byte[10240];
				InputStream in= rs3.getBinaryStream(1);	
				int n=in.read(b);
				out.println(n);
				in.close();
				
				Image img=Toolkit.getDefaultToolkit().getImage("C:/Documents and Settings/CEAdmin.SERVER17/Desktop/ca.gif");
				
				out.println(img);

The error displayed is: sun.awt.image.ToolkitImage@176e9c0

and what does this have to do with JSP?
Or haven't you gotten the hint yet that you should never use Java code in JSP?
And the message that you should never put binary content in a JSP outputstream?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.