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


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


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

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

3. สร้าง code สำหรับการบันทึกข้อมูล binary ลงฐานข้อมูล ดังนี้
private void SavePicture() {
Connection conn = null;
try {
conn = getConnection();
//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;
PreparedStatement prepStmt = conn.prepareStatement("Insert into test values(?,?)");
prepStmt.setString(1, file.getName());
prepStmt.setInt(2, oid);
} catch( Exception e ) {

4. สร้าง code สำหรับการดึงหรืออ่านข้อมูล binary จากฐานข้อมูล ดังนี้
public byte[] getData(){
Connection conn = null;
Vector keep = new Vector();
byte buf[] = null;
try {
conn = getConnection();
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);
LargeObject obj = manager.open(oid, LargeObjectManager.READ);
//Read the data
buf = new byte[obj.size()];
obj.read(buf, 0, obj.size());
result = null;
prepStmt = null
; } catch(Exception e) {
System.out.println("Error in getData() method > " + e.getMessage());
} finally {
try {
if(conn != null) {
conn = null;
} catch(SQLException sqle) {
System.out.println("Error in getData() method > " + sqle.getMessage());
return buf;

//SErvlet Code
//Set contenttype of response
OutputStream out = response.getOutputStream();




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
ugg boots,uggs outlet,ugg,uggs,ugg boots clearance,uggs on sale,uggs boots,ugg australia,ugg slippers grabbed the leather bound room service menu
cheap jerseys from beside
true religion outlet the table. "Let's order filet mignons
burberry uk and French fries and
lululemon beer
dansko shoes and watch
jimmy choo,jimmy choo shoes,jimmy choo outlet,jimmy choo handbags old
adidas movies!" She'd
roshe run always
bebe dresses been
burberry an expert
ray ban sunglasses at changing the subject. Blair scooted her feet up underneath
beats by dre,dr dre,beats headphones,dre beats,beats by dr,beats by dr dre,beats audio,dr dre beats,dre headphones,beats by dre headphones,beats by dr. dre,cheap beats her and
burberry,burberry outlet,burberry outlet online,burberry factory outlet,burberry sale,burberry handbags reached for
ray ban sunglasses the
new balance shoes TV
oakley sunglasses,cheap oakley,cheap oakley sunglasses,oakley sunglasses cheap,oakley outlet,oakley sunglasses outlet,oakley vault,oakleys,oakley.com,sunglasses outlet,cheap sunglasses,oakley prescription glasses,fake oakleys,