| 发表于:2007-12-06 22:59:591楼 得分:0 |
给你一个例子看看 samples\java\jdbc\dtlob.java import java.io.*; import java.lang.*; import java.util.*; import java.sql.*; class dtlob { public static void main(string argv[]) { try { db db = new db(argv); system.out.println(); system.out.println("this sample shows how to use lob data type."); // connect to the 'sample' database db.connect(); blobfileuse(db.con); // disconnect from the 'sample' database db.disconnect(); } catch (exception e) { jdbcexception jdbcexc = new jdbcexception(e); jdbcexc.handle(); } } // main static void blobfileuse(connection con) { try { system.out.println(); system.out.println( "----------------------------------------------------------\n" + "use the sql statements:\n" + " select\n" + " insert\n" + " delete\n" + "to show how to use binary large object (blob) files."); string osname = system.getproperty("os.name"); string photoformat; string filename; string empno; if (osname.equals("windows nt")) { photoformat = "bitmap"; filename = "photo.bmp"; } else { // unix photoformat = "gif"; filename = "photo.gif"; } // ---------- read blob data from file ------------------- system.out.println(); system.out.println( " ---------------------------------------------------\n" + " read blob data from the file '" + filename + "':"); system.out.println(); system.out.println( " prepare the statement:\n" + " select picture\n" + " from emp_photo\n" + " where photo_format = ? and empno = ?"); preparedstatement pstmt = con.preparestatement( "select picture " + " from emp_photo " + " where photo_format = ? and empno = ?"); system.out.println(); system.out.println( " EXECute the prepared statement using:\n" + " photo_format = 'bitmap'\n" + " empno = '000130'"); empno = "000130"; pstmt.setstring(1, photoformat); pstmt.setstring(2, empno); resultset rs = pstmt.EXECutequery(); rs.next(); blob blob = rs.getblob(1); rs.close(); pstmt.close(); system.out.println(); system.out.println(" read from blob file successfully!"); // -------------- write blob data into file ----------------- system.out.println(); system.out.println( " ---------------------------------------------------\n" + " insert blob file " + filename + " into the db:"); system.out.println(); system.out.println( " prepare the statement:\n" + " insert into emp_photo(photo_format, empno, picture)\n" + " values (?, ?, ?)"); preparedstatement pstmt2 = con.preparestatement( "insert into emp_photo (photo_format, empno, picture) " + " values (?, ?, ?)"); system.out.println(); system.out.println( " EXECute the prepared statement using:\n" + " photo_format = 'bitmap'\n" + " empno = '000137'\n" + " and the blob object that we get from reading the\n" + " file 'photo.*' eariler."); empno = "000137"; pstmt2.setstring(1, photoformat); pstmt2.setstring(2, empno); pstmt2.setblob(3, blob); pstmt2.EXECuteupdate(); pstmt2.close(); system.out.println(); system.out.println(" insert blob file to db successfully!"); // ------------ delete new record from the database --------- system.out.println(); system.out.println( " ---------------------------------------------------\n" + " delete the new record from the database:"); system.out.println(); system.out.println( " prepare the statement:\n" + " delete from emp_photo where empno = ?"); preparedstatement pstmt3 = con.preparestatement( "delete from emp_photo where empno = ? "); system.out.println(); system.out.println( " EXECute the prepared statement using:\n" + " empno = '000137'"); pstmt3.setstring(1, empno); pstmt3.EXECuteupdate(); pstmt3.close(); system.out.println(); system.out.println(" delete the new record from db successfully!"); } catch (exception e) { jdbcexception jdbcexc = new jdbcexception(e); jdbcexc.handle(); } } // blobfileuse } // dtlob class | | |
|