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