JDBC inserts large objects and reads large objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.