Java writes text, ntext, and image fields in SQL Server

Source: Internet
Author: User
Java writes text, ntext, and image fields to the SQL Server database. When the data volume is too large, you can use the updatetext statement to insert data in segments.

 

Person ;//ThePersonObject

Inputstream input; // personThePhoto, Obtained through webpage upload, insertImageType field

ConnectionConn = new connection ();//ActualConnectionObject

Preparedstatement stmt = NULL;

Resultset rs = NULL;

String SQL= NULL;

 

Conn.Setautocommit(False );//SetAuto commit = false;

SQL = "insert into person (ID, name) values (?, ?) ";//InsertPersonOther fields

Stmt = Conn.Preparestatement(SQL );

Stmt. setint (1, person. ID );

Stmt. setstring (2, person. Name );

Stmt.Executeupdate();

 

SQL = "UpdatePersonSetPhoto= NULL where id =?";//InitializationImageData Type pointer

//Even inInsertInsertImageType value:NullThe database still does not create pointers. AvailableUpdateCreate a pointer.

Stmt = Conn.Preparestatement(SQL );

Stmt. setint (1, person. ID );

Stmt.Executeupdate();

 

SQL = "select textptr (Photo) FromPersonWhere id =?";//ObtainImageType pointer

Stmt = Conn.Preparestatement(SQL );

Stmt. setint (1, person. ID );

Rs =Stmt. Executequery (SQL );

If (Rs. Next ())

{

Byte [] PTR = Rs. getbytes (1 );// PTR:ImageType pointer

SQL = "updatetextPerson.Photo? ? 0? ";//UpdateImageType field,0Indicates that the content is not deleted.

Stmt = conn. preparestatement (SQL );

Int offset = 0;

Int Len = 0;

Byte [] buffer = new byte [10*1024];//Read/write buffer

While (LEN = input. Read (buffer ))! =-1)//ReadInputStream,-1Indicates that you have read

{

Stmt. setbytes (1, PTR );// ImageType pointer

Stmt. setint (2, offset );//Offset

Stmt. setbytes (3, buffer );//Read content

Stmt.exe cuteupdate ();//Write read content to the database

Stmt. clearparameters ();

Offset + = Len;//Offset

}

SQL = "updatetextPerson.Photo? ? Null ";//Delete unnecessary content,0All content after offset

Stmt = conn. preparestatement (SQL );

Stmt. setbytes (1, PTR );

Stmt. setint (2, offset );//Offset of the end point

Stmt.exe cuteupdate ();

 

Input. Close ();//Close input stream

}

Conn. commit (); // commit connection

Related Article

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.