Environment

Document Storing Binary in PostGreSQL http://www.postgresql.org/docs/7.4/static/jdbc-binary-data.html JDBC Driver for PostGreSQL http://jdbc.postgresql.org/download.html Document of JDBC http://jdbc.postgresql.org/documentation/pgjdbc.html

Requirement

ต้องการบันทึกแสดงแสดงข้อมูลชนิด Binary ที่เก็บในฐานข้อมูล PostGreSQL

Solution

1. สร้าง Database ที่สนับสนุน Unicode Character Encoding 2. สร้าง table สำหรับเก็บข้อมูลชนิด binary ดังนี้

CREATE TABLE test(
name text, //ชื่อ file
pic oid //เก็บข้อมูล binary)

3. สร้าง code สำหรับการบันทึกข้อมูล binary ลงฐานข้อมูล ดังนี้
private void SavePicture() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
//Create Large object manager
LargeObjectManager manager = ( ( PGConnection ) conn ).getLargeObjectAPI();
int oid = manager.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
LargeObject obj = manager.open(oid, LargeObjectManager.WRITE);
//Read picture from file
File file = new File("P1020156.JPG");
FileInputStream in = new FileInputStream(file);
//Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = in.read(buf, 0, 2048)) > 0) {
obj.write(buf, 0, s);
tl += s;
}
obj.close();
PreparedStatement prepStmt = conn.prepareStatement("Insert into test values(?,?)");
prepStmt.setString(1, file.getName());
prepStmt.setInt(2, oid);
prepStmt.executeUpdate();
prepStmt.close();
in.close();
conn.commit();
conn.setAutoCommit(true);
conn.close();
} catch( Exception e ) {
e.printStackTrace();
}
}

4. สร้าง code สำหรับการดึงหรืออ่านข้อมูล binary จากฐานข้อมูล ดังนี้
public byte[] getData(){
Connection conn = null;
Vector keep = new Vector();
byte buf[] = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
LargeObjectManager manager = ( ( PGConnection ) conn ).getLargeObjectAPI();
PreparedStatement prepStmt = conn.prepareStatement("select pic from test");
ResultSet result = prepStmt.executeQuery();
if(result.next()) {
int oid = result.getInt(1);
System.out.println(oid);
LargeObject obj = manager.open(oid, LargeObjectManager.READ);
//Read the data
buf = new byte[obj.size()];
obj.read(buf, 0, obj.size());
obj.close();
}
result.close();
result = null;
prepStmt.close();
prepStmt = null
; } catch(Exception e) {
System.out.println("Error in getData() method > " + e.getMessage());
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch(SQLException sqle) {
System.out.println("Error in getData() method > " + sqle.getMessage());
sqle.printStackTrace();
}
}
return buf;
}

//SErvlet Code
//Set contenttype of response
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
out.write(getData());
out.flush();
out.close();

Comment

Comment:

Tweet

Then
salomon shoes Serena giggles. "I guess
nike mercurial it would
michael kors,sac michael kors,michael kors sac,michael kors pas cher,sac michael kors pas cher,michael kors france have
cartier been more fun
bebe dresses,bebe clothing,bebe outlet,bebe outlet online,bebe stores if I'd really been
michael kors outlet online,michael kors,kors outlet,michael kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors Stan
toms shoes,toms outlet,toms shoes outlet,tom shoes,toms wedges,cheap toms,toms.com 5."
designer handbags,handbags outlet,purses and handbags,wholesale handbags,designer bags,bags outlet,handbags wholesale,cheap purses,discount handbags,cheap handbags,handbags sale,wholesale purses,handbags and purses,designer purses Blair didn't
kobe x say anything. She'd basically
eyeglass frames,eyeglasses online,glasses frames,glasses online,eyeglasses frames,eyeglasses stores,cheap eyeglasses,cheap glasses,sunglasses for women,39 dollar glasses,cheap eyeglasses online,discount eyeglasses,cheap sunglasses,sunglasses outlet,sunglasses wholesale,wholesale sunglasses just broken
uggs outlet up
air max 90 with Nate-
marc jacobs handbags,marc jacobs,marc by marc jacobs,marc jacobs outlet again- after passing up a perfectly
toms shoes good opportunity to
levi's jeans fool
michael kors around with
cheap basketball shoes a boy who could very well
birkin bag,hermes belt,hermes handbags,hermes birkin,hermes bags,birkin bags get
reebok shoes her into Yale. Well,
nike blazer,blazer nike,nike blazer pas cher,Chaussures Nike Blazer,Nike Blazer Femme one thing
polo ralph lauren,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,polo ralph lauren outlet online,ralph lauren,polo ralph,polo shirts,ralphlauren.com,polo outlet,ralph lauren polo was for sure:
chanel handbags,chanel bags,chanel sunglasses,chanel outlet,chanel purses,chanel handbags official site She wasn't about
timberland boots to
cheap eyeglasses let
guess factory Stan
wedding dresses 5
tory burch get away.
chanel handbags Serena threw
harrods london back
omega watches,omega watch,replica watches,rolex watches,replica omega watches,rolex,watches for men,watches for women,rolex watches for sale,rolex replica,rolex watch,cartier watches,rolex submariner,fake rolex,rolex replica watches,replica rolex the covers and