0

displaying image from database

asked 2011-12-13 15:14:01 +0800

raviteja gravatar image raviteja
90

i am saving the image into database sucessfully...........................
noe i want to retrive it and display in the zul page....................can any body help me please..............................with code

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-12-13 15:52:08 +0800

yaminglin1977 gravatar image yaminglin1977
36 2

updated 2011-12-13 15:53:39 +0800

suppose get pic data using getFromDb method,

  
			byte[] bytes =getFromDb();
			try {
				AImage aImage = new AImage("abc.jpg", bytes);
				image.setContent(aImage);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

}

link publish delete flag offensive edit

answered 2011-12-20 05:34:57 +0800

amolbote gravatar image amolbote
21

Index.zul
_______________________________________________________
<?page title="Auto Generated index.zul"?>

<window id="windowFlash" title="" border="normal" width="700px" apply="test.IndexControl">

<fileupload label="Upload" id="btnUpload" />

<image id="myimage" width="500px" height="500px"></image>

</window>

_________________________________________________
InexControl.java
__________________________________________
package test;

import java.io.IOException;
import org.zkoss.image.AImage;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Fileupload;
import org.zkoss.zul.Image;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class IndexControl extends GenericForwardComposer {

Fileupload btnUpload;
Button btnMultiUpload, btnPreview;
Button btnDelete, btnInsert;

Window windowFlash;
Image myimage;

BlobTest blobTest = new BlobTest();

public void onUpload$btnUpload(UploadEvent event) throws IOException {

org.zkoss.util.media.Media media = event.getMedia();

if (media instanceof org.zkoss.image.Image) {

blobTest.insertBlob(media.getName(), media.getStreamData());

byte[] b = blobTest.writeBlob(media.getName());
AImage newimage = new AImage("myimage.jpg", b);

myimage.setContent(newimage);
myimage.invalidate();
} else {
try {
Messagebox.show("Not an image: " + media, "Error",
Messagebox.OK, Messagebox.ERROR);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

}

_______________________________________________________________
BlobTest.java
________________________________________________

package test;

import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BlobTest {

public void insertBlob(String file_name, InputStream stream) {
Connection conn = null;
try {
conn = getConnection();

if (stream != null) {

System.out.println("..........." + stream.available());

PreparedStatement ps = conn
.prepareStatement("INSERT INTO MULTIMEDIA_MASTER(FILE_NAME, FILE_DATA) VALUES(?, ?)");
ps.setString(1, file_name);

ps.setBinaryStream(2, stream, stream.available());

ps.executeUpdate();
ps.close();
System.out.println("...record inserted");
} else {
PreparedStatement ps = conn
.prepareStatement("INSERT INTO MULTIMEDIA_MASTER(FILE_NAME, FILE_DATA) VALUES (?, empty_blob())");
ps.setString(1, file_name);
ps.execute();
ps.close();

}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public Connection getDBConnection() {
Connection co = null;
try {
co = getConnection();
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}
return co;

}

public byte[] writeBlob(String filename) {
Connection conn = null;
InputStream mystream = null;
byte[] data = null;
try {

conn = getConnection();
Statement st = conn.createStatement();
ResultSet rs = st
.executeQuery("SELECT FILE_DATA FROM MULTIMEDIA_MASTER where FILE_NAME ='"
+ filename + "' FOR UPDATE");
while (rs.next()) {
Blob blob = rs.getBlob(1);
System.out.println("length of a blob....." + blob.length());
data = blob.getBytes(1, (int) blob.length());
}

} catch (Exception e) {
e.printStackTrace();
}
return data;
}

private Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "abcd", "root");
return conn;
}

}

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2011-12-13 15:14:01 +0800

Seen: 345 times

Last updated: Dec 20 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More