Direct and Lower code:
1 Packagecom.learn.jdbc.chap06;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.InputStream;6 Importjava.sql.Connection;7 Importjava.sql.PreparedStatement;8 9 ImportCom.learn.jdbc.model.Album;Ten ImportCom.learn.jdbc.util.DbUtil; One A /** - * Processing Big Data Objects Clob data and BLOB data--inserting - * @authorAdministrator the * - */ - Public classDemo1 { - Private StaticDbutil dbutil=Newdbutil (); + - Private Static intAddalbum (Album AB)throwsexception{ +Connection con =Dbutil.getcon (); AString sql= "INSERT into sp_album_test values (null,?,?,?,?,?)"; atPreparedStatement pstmt =con.preparestatement (SQL); -Pstmt.setstring (1, Ab.getname ()); -Pstmt.setint (2, Ab.getuid ()); -Pstmt.setlong (3, Ab.gettime ()); - - //Large character data (Clob data) inserted into the database--text file inFile content =ab.getcontent (); -InputStream InputStream =NewFileInputStream (content); toPstmt.setasciistream (4, InputStream, Content.length ()); + - //Binary data (BLOB data) inserting a database-pictures, movies, music theFile pic =ab.getpic (); *InputStream inputStream2 =Newfileinputstream (pic); $Pstmt.setbinarystream (5, InputStream2, Pic.length ());Panax Notoginseng - intresult =pstmt.executeupdate (); the dbutil.close (pstmt, con); + returnresult; A } the + Public Static voidMain (string[] args)throwsException { -File content =NewFile ("E:/code.txt"); $File pic =NewFile ("E:/koala111.png"); $Album AB =NewAlbum ("Zhang San", 1, System.currenttimemillis (), content, pic); - intresult =Addalbum (AB); - if(result>0){ theSYSTEM.OUT.PRINTLN ("Data insertion succeeded"); -}Else{WuyiSYSTEM.OUT.PRINTLN ("Data insertion failed"); the } - } Wu}
1 Packagecom.learn.jdbc.chap06;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 ImportJava.io.OutputStream;6 ImportJava.sql.Blob;7 ImportJava.sql.Clob;8 Importjava.sql.Connection;9 Importjava.sql.PreparedStatement;Ten ImportJava.sql.ResultSet; One A ImportCom.learn.jdbc.util.DbUtil; - - the /** - * Processing Big Data Objects Clob data and BLOB data--query - * @authorAdministrator - * + */ - Public classDemo2 { + Private StaticDbutil dbutil=Newdbutil (); A at Private Static voidListalbum (intIdthrowsexception{ -Connection con =Dbutil.getcon (); -String sql= "SELECT * from Sp_album_test where id=?"; -PreparedStatement pstmt =con.preparestatement (SQL); -Pstmt.setint (1, id); -ResultSet rs =pstmt.executequery (); in - if(Rs.next ()) { toString name = rs.getstring ("name"); + intUID = rs.getint ("UID"); - LongTime = Rs.getlong ("Add_time"); the //read large characters data--clob data *Clob Clob =rs.getclob ("content"); $String content = clob.getsubstring (1, (int) clob.length ());Panax Notoginseng - //reading binary data--blob data theBlob blob = Rs.getblob ("Pic"); +OutputStream out =NewFileOutputStream (NewFile ("E:/kaola.png")); AOut.write (Blob.getbytes (1, (int) (Blob.length () )); the out.close (); + -System.out.println ("Name:" +name); $System.out.println ("UID:" +uid); $System.out.println ("Time:" +Time ); -System.out.println ("Content:" +content); - the - }Wuyi the } - Wu - Public Static voidMain (string[] args)throwsException { AboutListalbum (1); $ } -}
Working with big Data Objects Clob data and BLOB data