Display Blob[Image] from Mysql table using JSP
In this section, we will display blob data[image] from Mysql database table using JSP code.
In this section, we will display blob data[image] from Mysql database table using JSP code.
Display Blob[Image] from Mysql table using JSP
In this section, we will display blob data[image] from Mysql database table using JSP code.
A Blob stores a binary large object in the database table's row. Blob object contains a logical pointer which points to the Blob data, data is not directly stored in the row of the database table. Blob object is valid for the duration of the transaction. The "getBlob[]" and "setBlob[]" method of "ResultSet" ,"CallableStatement", and "PreparedStatement" interface , is used for accessing Blob value.
The Method defined in " java.sql.Blob " interface is as follows--
Return Type
Method Description InputStream getBinaryStream[]
Retrieves the blob value designated
by this blob value as a stream
byte[] getBytes[long pos, int length] Retrieve all or part of the blob value that
this blob represents as an array of bytes.
long length[] Returns the no of bytes in the Blob
value designated by this Blob object
OutputStream setBinaryStream[long pos] Retrieves a stream that can be used
to write to the Blob value
int setBytes[long pos, byte[] bytes] Write the given array of bytes to the Blob
value that this Blob object represent
starting at position pos, and returns the no
of bytes written
void & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; cắt ngắn [len dài] & nbsp; & nbsp; & nbsp; ; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; đối tượng đại diện cho là len byte trong Len truncate[long len] Truncates the blob value that this blob
object represents to be len bytes in len
Kiểu dữ liệu Blob có thể được phân loại thành bốn loại--
- TinyBlob: Độ dài tối đa là 255 ký tự [8 bit]: The maximum length is 255 characters [8 bits]
- BLOB: Độ dài tối đa là 16,535 ký tự [16 bit]: The maximum length is 16,535 characters [16 bits]
- Mediumblob: Độ dài tối đa là 16.777.216 ký tự [24 bit]: The maximum length is 16,777,216 characters [24 bits]
- Longblob: Độ dài tối đa là 4.294.967.295 ký tự [32 bit].: The maximum length is 4,294,967,295 characters [32 bits].
THÍ DỤ :
displayblob.jsp
pageImport = "java.io.*"%> import="java.sql.*"%> pageImport = "java.io.*"%> Kết nối con = Câu lệnh stmt = Resultset rs = Class.forname [ con = DriverManager.getConnection [ stmt = con.CreateStatement []; rs = stmt.executequery [ hình ảnh = rs.getBlob [1]; imgdata = Image.getBytes [1, [
out.println[
"Display Blob Example"];out.println[
"image not found for given id>"]; return;}
// display the imageresponse.setContentType[
"image/gif"];}
o.write[imgData];
o.flush[];
o.close[];
imgdata = Image.getBytes [1, [
catch [Exception e] {out.println[
"Unable To Display image"];out.println[
"Image Display Error=" + e.getMessage[]]; return;imgdata = Image.getBytes [1, [
finally { try {rs.close[];
stmt.close[];
con.close[];
imgdata = Image.getBytes [1, [
catch [SQLException e] {e.printStackTrace[];
}
}
%>}
OutputStream o = respons.getOutputStream []; Insert Blob[Image] in Mysql table using JSP
Đầu ra: