When everyone wants to complete a task, the first is to consider how to design, (will be said to be soft). When it is realized, it is definitely a reuse of ready-made code, or finds a similar reference. See some of the articles left a lot of code, that is, there are also many high-score code (not an urgency). Therefore, I will write yourself more typical useful code to everyone, for your reference, limited horizontal, hopes to give more opinions, 咔咔
This time is the preservation of the picture. In the picture in SQL 2K, the picture is generally used as image or varbinary type. When the operation, the feeling of the feeling is very confused, it will be very simple. Through your own experience, it is most convenient to save through byte arrays, and it is not easy to make mistakes. Through the flow mode, there will always be some problems. When getting a picture or file, both is relatively stable, whether using Microsoft's JDBC driver or using a JDBCODBC bridge.
/ * * Created on 2003-5-13 * * to change the Template for this generated file go to * window> preferences> java> code generation> code and comments * / package scut.ailab.sql2ktools;
/ ** * @AuThor YouYongming * For the IMGE and VARBINARY for the database field * / import java.sql. *; Import java.io. *; Import java.nio. *;
Public class imageoperty {// Define Database Connection Private Conn; / ** * Use the specified connection constructor * @Param conn database connection * / public imageoperty (connection conn) {this.conn = conn; if (this.conn == NULL) // If there is no initialization connection, INITCONN ();} / ** * default database connection * When constructor's conn is called, do demonstration with * / private void initconn () {String user = " DevTeam "; String password =" DevTeam "; String driverstr =" sun.jdbc.odbc.JdbcOdbcDriver "; String url =" jdbc: odbc: chinascutface "; try {Class.forName (driverstr) .newInstance (); conn = DriverManager .getConnection (URL, User, Password);} catch (exception e) {E.PrintStackTrace ();}} / ** * Save Image file to the database specified by the database * @Param SQLSTR Specified SQL statement * @Param File Specifying image file * @return Save successfully returns true, otherwise returns false * / public boilean storeImage (string sqlstr, file file) {Try {// Open file fileInputStream FIN = new fileInputStream (file); // Build a buffer save data Bytebuffer NBF = Bytebuffer.allocate ((int) file.Length ()); byte [] array = new byte [1024]; int offset = 0, length = 0; // read data while ((length = fi) N.read (array))> 0) {//nbf.put (Array, offset, length); if (length! = 1024) NBF.PUT (Array, 0, Length); Else NBF.PUT (Array); OFFSET = Length;} // Close file Fin.close (); // Newly built an array save you want to write Byte [] content = nbf.Array (); // Save Data Return SetImage (Sqlstr, Content); Catch (filenotfoundexception e) {E.PrintStackTrace ();} catch (ooException e) {E.PrintStackTrace ();} // If a file read and write error will return false return false;} / ** * Save byte array to Database Specify Location * @Param Sqlstr Description Location SQL Statement * @Param in To save byte array * @
Save return returns success * / private boolean setImage (String sqlstr, byte [] in) {boolean flag = false; if (sqlstr == null) sqlstr = "Select img from image_table"; try {Statement stmt = conn.createStatement ( ResultSet.Type_scroll_insensitive, resultoutet.concur_updatable); ResultSet RS = Stmt.executeQuery (SQLSTR); if (rs.next ()) {Rs.UpdateBytes (1, in); rs.Updaterow ();} ket.close (); Flag = true;} catch (exception e) {E.PrintStackTrace ();} return flag;} / ** * Note location from the database * @Param SQLSTR Description Database location SQL statement * @Param file image file save * @return return path is correct file * / public boolean retrieveImage (String sqlstr, file file) {boolean flag = false; if (sqlstr == null) sqlstr = "select img from image_table"; try {Statement stat = conn.createStatement (); ResultSet RS = stat.executeQuery (sqlstr); while (rs.next ()) {byte [] content = rs.getbytes (1); if (! Rs.wasnull ()) // Find Data {Flag = True; // Save to the specified file fileOutputStream Fout = New FileoutputStream (file) Fout.write (Content); fout.flush (); fout.close ();} // Save the first record, jump out of Break;} // Turn off rs.close (); stat.close () ;} Catch (sqlexception SQLE) {sqle.printStackTrace ();} catch (exception e) {E.PrintStackTrace (); flag = false; // If there is an IO error, it is unsuccessful} Return flag;} // Test public static Void main (string [] args) {imageOperation IOP = new imageoperty (null); try {// uses a picture as a test file File File = New File ("Wodao.gif"); if (Iop.StoreImage (Null, File )) System.out.print ("true"); Else System.Out.print ("false"); file = new file ("new.gif");