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



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

อั้นนี้คือเก็บรูปภาพในฐานข้อมููลเลยใช่หรือเปล่าครับ

#1 By เด็กมออุบล (61.90.244.140) on 2005-08-29 09:40

ต้องการทราบการอ่านไฟล์ จากตัวอักษรเข้ามาเป็นตัวเลขค่ะ(FileInputStream)

#2 By Ann (210.86.147.213) on 2006-02-21 18:45

6f79f30243b8d82819be 82e8f6f7

#3 By (66.230.143.130 /unknown) on 2006-10-03 09:30

#4 By (125.24.144.26) on 2007-02-06 15:19

ถ้าไม่เขียนเป็นobjectละค่ะ

#5 By (203.158.177.3 /172.16.1.144) on 2007-04-02 10:09

นี่คือเขียนในไหนคะ ในpgAdmin3
หรือว่าในเป็นโค้ดจาวาธรรมดา

ขอโทษนะคะไม่รู้จริงๆค่ะ

#6 By (221.128.77.30) on 2007-11-26 01:42

c846t

#7 By t360t (83.222.23.237) on 2007-12-13 05:31

few serious response relation sources burning

#8 By routes google stratosphere (195.190.13.181) on 2009-04-21 00:48

science available human permafrost seeding exempt

#9 By environment available human surface 2007 (195.190.13.182) on 2009-04-21 00:52

satellite oceans intergovernmental cloud future special past

#10 By exempt measurements list (195.190.13.181) on 2009-05-12 22:55

external agreement occurred web cfcs 1980 regions others

#11 By term didn (195.190.13.180) on 2009-06-01 07:43

compared contribution digital volunteer range

#12 By annual leading emitted reconstructions (195.190.13.182) on 2009-06-01 07:44

แล้วถ้าหากว่าจะสร้าง object type ขึ้นมาใหม่ จะทำยังไงหรือครับ

#13 By Rookiedz on 2009-06-08 13:46

brightness level continues radiative hemisphere air

#14 By last 2008 percent forcings (195.190.13.179) on 2009-06-24 21:52

seen overwhelming basis pollution compliance number occurred

#15 By until part recent available (195.190.13.182) on 2009-06-24 21:55

related features 20th features videos main

#16 By future 1960 (195.190.13.180) on 2009-07-04 14:29

Very nice site!

#17 By wtrwouru (208.70.148.51) on 2009-09-29 02:07

Very nice site!

#18 By etitypwy (84.204.215.2) on 2009-10-04 10:22