Accessing BLOB in MySQL in Java

Source: Internet
Author: User
During webpage creation, the string storage length in MySQL is 255, so blob can be used for long text. However, I find it hard to find the answer to this problem on the Internet. Here, I will write out my solution.
I am working on a message book, the code used in JSP is <textarea Cols = "70" Title = "message content" rows = "15" name = "wordco"> </textarea>. Wordco is of the string type. The input content is stored in wordco. After clicking the submit button, JSP calls a function in bean to add this message.
In bean, convert the string in wordco into a byte array: byte [] BSTR = wordco. getbytes (); then convert it to a binary stream: bytearrayinputstream Bis = new bytearrayinputstream (BSTR); then
Con = getconnection ();
String appendstatement = "insert into words values ('" + wordid + "', '" + wordna + "', '" + wordte + "', '"+ wordem + "',?, '"+ Wordti + "')";
Prepstmt = con. preparestatement (appendstatement );
Prepstmt. setbinarystream (1, BIS, BSTR. Length );
Prepstmt.exe cuteupdate ();
The above is saved.

The output is made in JSP. The Code is as follows:
<TD bgcolor = "# ccccff" width = "55%"> <B> <%
Inputstream in = Rs. getbinarystream ("wordco ");
Bytearrayoutputstream outp = new bytearrayoutputstream ();
Int C;
While (C = in. Read ())! =-1 ){
Outp. Write (C );
}
String wordco = outp. tostring ();
In. Close ();
Outp. Close ();
%> <% = Convertup (wordco) %> </B> </TD>
Note: convertup is used to solve the problem of Chinese transcoding and has nothing to do with BLOB access.
The above is the result.
 
If the above Code has a problem, please leave a message below

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.