1 /**2 * Insert Large Objects3 */4 @Test5 Public voidTestsavepic () {6Connection conn=NULL;7Statement st=NULL;8PreparedStatement pst=NULL;9 Try {Tenconn=dbutils.getconn (); OneConn.setautocommit (false);//things can't be submitted automatically Ast=conn.createstatement (); - LongStart =System.currenttimemillis (); -String sql= "INSERT into Stu (Sid,name,pic,info) VALUES (?,?,?,?)"; thepst=conn.preparestatement (SQL); -Pst.setint (1, 1); -Pst.setstring (2, "Tim"); - //Set Large Objects +File File =NewFile ("D:\\arch\\unarch/b.jpg"); -FileInputStream FIS =Newfileinputstream (file); +Pst.setbinarystream (3, Fis,file.length ());//set the binary stream, specifying the length APst.setstring (4, "xxxxxx"); at pst.executeupdate (); -Conn.commit ();//Commit a transaction -System.out.println (System.currenttimemillis ()-start); -}Catch(Exception e) { - //TODO auto-generated Catch block - e.printstacktrace (); in } - finally{ toDbutils.closeall (NULL, ST, conn); + } -}
2. Read Large objects
1 /**2 * For fetching large objects3 */4 @Test5 Public voidTestreadpic () {6Connection conn=NULL;7Statement st=NULL;8PreparedStatement pst=NULL;9 Try {Tenconn=dbutils.getconn (); OneConn.setautocommit (false);//things can't be submitted automatically Ast=conn.createstatement (); - LongStart =System.currenttimemillis (); -String sql= "Select pic from Stu where sid=?"; thepst=conn.preparestatement (SQL); -Pst.setint (1, 1); -ResultSet rs =pst.executequery (); - if(Rs.next ()) { + byte[] bytes = rs.getbytes (1); -FileOutputStream fos =NewFileOutputStream ("D:/kk.jpg"); + fos.write (bytes); A fos.close (); at } - -Conn.commit ();//Commit a transaction -System.out.println ("Over"); -System.out.println (System.currenttimemillis ()-start); -}Catch(Exception e) { in //TODO auto-generated Catch block - e.printstacktrace (); to } + finally{ -Dbutils.closeall (NULL, ST, conn); the } *}
JDBC inserts large objects and reads large objects