Java read blob full body garbled

Source: Internet
Author: User

one, BLOB Operations1, Warehousing (1) JDBC Mode//getting a database connection through JDBCClass.forName ("Oracle.jdbc.driver.OracleDriver"); Connection Con= Drivermanager.getconnection ("jdbc:oracle:thin: @localhost: 1521:testdb","Test","Test"); Con.setautocommit (false); Statement St=con.createstatement (); //insert an empty object Empty_blob ()St.executeupdate ("INSERT INTO Testblob (ID, NAME, blobattr) values (1,"Thename", Empty_blob ())"); //Lock the data row for updates, and note the "for update" statementResultSet rs = st.executequery ("Select Blobattr from Testblob where id=1 for update"); if(rs.next ()) {//cast to Oracle.sql.BLOB after getting Java.sql.Blob objectOracle.sql.BLOB BLOB = (oracle.sql.BLOB) Rs.getblob ("blobattr"); OutputStream OutStream=Blob.getbinaryoutputstream (); //data is an incoming byte array, defined by: byte[] dataOutstream.write (data,0, data.length);      } Outstream.flush ();      Outstream.close ();      Con.commit ();  Con.close (); (2) Jndi mode//getting a database connection through JndiContext context =NewInitialContext (); DS= (DataSource) Context.lookup ("Ora_jndi"); Connection Con=ds.getconnection (); Con.setautocommit (false); Statement St=con.createstatement (); //insert an empty object Empty_blob ()St.executeupdate ("INSERT INTO Testblob (ID, NAME, blobattr) values (1,"Thename", Empty_blob ())"); //Lock the data row for updates, and note the "for update" statementResultSet rs = st.executequery ("Select Blobattr from Testblob where id=1 for update"); if(rs.next ()) {//cast to Weblogic.jdbc.vendor.oracle.OracleThinBlob after getting Java.sql.Blob object (different app server may be Different)Weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) Rs.getblob ("blobattr"); OutputStream OutStream=Blob.getbinaryoutputstream (); //data is an incoming byte array, defined by: byte[] dataOutstream.write (data,0, data.length);      } Outstream.flush ();      Outstream.close ();      Con.commit ();  Con.close (); 2, out of the library//get a database connectionConnection con =connectionfactory.getconnection (); Con.setautocommit (false); Statement St=con.createstatement (); //no "for update" requiredResultSet rs = st.executequery ("Select Blobattr from Testblob where id=1"); if(rs.next ()) {java.sql.Blob Blob= Rs.getblob ("blobattr"); InputStream instream=Blob.getbinarystream (); //data is read out and needs to be returned, the type is byte[]data =New byte[input.available ()];          Instream.read (data);  Instream.close (); <preclass="Java"Name="Code">conn = this. getconnection (); Conn.setautocommit (false); Java.sql.Statement St=conn.createstatement (); RS=St.executequery (sql); BLOB Inblob=NULL; if(rs.next ()) {inblob= (BLOB) Rs.getblob ("blobattr"); } Data=inblob.getbytes (1,(int) Inblob.length ());//This is the data .} instream.close ();      Con.commit ();     Con.close (); second, CLOB Operation1, Warehousing (1) JDBC Mode//getting a database connection through JDBCClass.forName ("Oracle.jdbc.driver.OracleDriver"); Connection Con= Drivermanager.getconnection ("jdbc:oracle:thin: @localhost: 1521:testdb","Test","Test"); Con.setautocommit (false); Statement St=con.createstatement (); //insert an empty object Empty_clob ()St.executeupdate ("INSERT INTO Testclob (ID, NAME, clobattr) values (1,"Thename", Empty_clob ())"); //Lock the data row for updates, and note the "for update" statementResultSet rs = st.executequery ("Select Clobattr from Testclob where id=1 for update"); if(rs.next ()) {//cast to Oracle.sql.CLOB after getting Java.sql.Clob objectOracle.sql.CLOB CLOB = (oracle.sql.CLOB) Rs.getclob ("clobattr"); Writer OutStream=Clob.getcharacteroutputstream (); //data is the passed-in string, defined: string Data        Char[] C =Data.tochararray (); Outstream.write (c,0, c.length);      } Outstream.flush ();      Outstream.close ();      Con.commit ();  Con.close (); (2) Jndi mode//getting a database connection through JndiContext context =NewInitialContext (); DS= (DataSource) Context.lookup ("Ora_jndi"); Connection Con=ds.getconnection (); Con.setautocommit (false); Statement St=con.createstatement (); //insert an empty object Empty_clob ()St.executeupdate ("INSERT INTO Testclob (ID, NAME, clobattr) values (1,"Thename", Empty_clob ())"); //Lock the data row for updates, and note the "for update" statementResultSet rs = st.executequery ("Select Clobattr from Testclob where id=1 for update"); if(rs.next ()) {//cast to Weblogic.jdbc.vendor.oracle.OracleThinClob after getting Java.sql.Clob object (different app server may be Different)Weblogic.jdbc.vendor.oracle.OracleThinClob Clob = (weblogic.jdbc.vendor.oracle.OracleThinClob) Rs.getclob ("clobattr"); Writer OutStream=Clob.getcharacteroutputstream (); //data is the passed-in string, defined: string Data        Char[] C =Data.tochararray (); Outstream.write (c,0, c.length);      } Outstream.flush ();      Outstream.close ();      Con.commit ();  Con.close (); 2, out of the library//get a database connectionConnection con =connectionfactory.getconnection (); Con.setautocommit (false); Statement St=con.createstatement (); //no "for update" requiredResultSet rs = st.executequery ("Select Clobattr from Testclob where id=1"); if(rs.next ()) {java.sql.Clob Clob= Rs.getclob ("clobattr"); Reader instream=Clob.getcharacterstream (); Char[] C =New Char[(int) Clob.length ()];          Instream.read (c); //data is read out and needs to be returned, type is stringdata =NewString (c);      Instream.close ();      } instream.close ();      Con.commit ();     Con.close (); Places to be aware Of:1, java.sql.Blob, oracle.sql.BLOB, weblogic.jdbc.vendor.oracle.OracleThinBlob Several types of differences2, java.sql.Clob, oracle.sql.CLOB, weblogic.jdbc.vendor.oracle.OracleThinClob Several types of differences in the use of company projects (blog): Warehousing: First insert a oracle.sql.CLOB.empty_lob () and then String updatebasesourcesql="Select content from Mb_basesource where ID =? for update"; Conn.setautocommit (false); PS=conn.preparestatement (updatebasesourcesql); Ps.setlong (1, result); ResultSet RS=Ps.executequery (); Oracle.sql.CLOB CLOB=NULL; if(rs.next ()) {clob= (oracle.sql.CLOB) Rs.getclob (1); } Writer WR=Clob.getcharacteroutputstream (); Wr.write (basesource[4]);      Wr.flush ();      Wr.close ();      Rs.close ();      Ps.close ();  Conn.commit (); Out of the library: findbasesourcesql="Select content from Mb_basesource where ID =?"; PS=conn.preparestatement (findbasesourcesql); Ps.setlong (1, sourceid); RS=Ps.executequery (); if(rs.next ()) {CLOB CLOB= (oracle.sql.CLOB) Rs.getclob (1); if(clob! =NULL) {Reader is=Clob.getcharacterstream (); BufferedReader BR=NewBufferedReader ( is); String s=Br.readline ();  while(s! =NULL) {result[6] +=s; S=Br.readline ();     }}} Rs.close ();     Ps.close ();  Conn.close (); 

Java read blob full body garbled

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.