在java中对mysql中blob型的存取

来源:互联网 发布:qt编程视频教程丁林松 编辑:程序博客网 时间:2024/05/23 20:03
   在做网页的时候,mysql中的string存储长度是255,于是对长文本可以使用blob。但是,如何实现长文本的存取,我发现这个问题在网上很难找到答案。这里,我把我的解决方法写出来。
    我做的是一个留言本,在jsp中用的相关代码是  <textarea cols="70" title="留言内容" rows="15" name="wordCo"></textarea> 。其中wordCo是string型,这里将输入的内容存入wordCo。在单击提交按钮之后,jsp调用bean中的一个功能,添加这条留言信息。
    在bean中,将wordCo里的字串转换成byte数组:byte [] bStr = wordCo.getBytes();然后转换为二进制流:ByteArrayInputStream bis = new ByteArrayInputStream(bStr);然后就是
   con = getConnection();
   String appendStatement = "insert into words values('" + wordId + "','" + wordNa + "','" + wordTe + "','" + wordEm + "',?,'" + wordTi + "')";
   prepStmt = con.prepareStatement(appendStatement);
   prepStmt.setBinaryStream(1,bis,bStr.length);
   prepStmt.executeUpdate();
   以上是存。
  
   输出是在jsp里做的,代码如下:
   <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>
    注:这里的convertup的功能是用于解决中文转码问题,与blob存取无关。
    上面是取。
 
    如果上面的代码有问题,请在下面留言 
原创粉丝点击