最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

mysql操作blob经验谈

日期:09-03    来源:中国设计秀    作者:cnwebshow.com

背景!jsp+MySQL 记住 要用mysql的longblob类型来存默认的blob大小不够BzO中国设计秀

数据库字段:id (char) pic (longblob)BzO中国设计秀

转载请注明出处,这时我与我的知己的合作的结过BzO中国设计秀

原来操作blob字段时都要先差个空值,在查blob,好麻烦,用prepareStatment就不用那么麻烦了,哈哈BzO中国设计秀

postblob.heml页面BzO中国设计秀

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">BzO中国设计秀
<html xmlns="http://www.w3.org/1999/xhtml">BzO中国设计秀
<head>BzO中国设计秀
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />BzO中国设计秀
<title>无标题文档</title>BzO中国设计秀
</head>BzO中国设计秀
<body>BzO中国设计秀
<form action="testblob.jsp" method="post" >BzO中国设计秀
<table width="291" border="1">BzO中国设计秀
  <tr>BzO中国设计秀
    <td width="107">id </td>BzO中国设计秀
    <td width="168"><input name="id" type="text" /></td>BzO中国设计秀
  </tr>BzO中国设计秀
  <tr>BzO中国设计秀
    <td>file</td>BzO中国设计秀
    <td><input name="file" type="file" /></td>BzO中国设计秀
  </tr>BzO中国设计秀
  <tr>BzO中国设计秀
    <td><input  type="submit"  value="提交"/></td>BzO中国设计秀
   BzO中国设计秀
  </tr>BzO中国设计秀
</table>BzO中国设计秀
</form>BzO中国设计秀
</body>BzO中国设计秀
</html>BzO中国设计秀
***************************************************************BzO中国设计秀

testblob.jspBzO中国设计秀

<%@ page contentType="text/html;charset=gb2312"%> BzO中国设计秀
<%@ page import="java.sql.*" %>BzO中国设计秀
<%@ page import="java.util.*"%>BzO中国设计秀
<%@ page import="java.text.*"%>BzO中国设计秀
<%@ page import="java.io.*"%> BzO中国设计秀
<html xmlns="http://www.w3.org/1999/xhtml">BzO中国设计秀
<head>BzO中国设计秀
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />BzO中国设计秀
<title>无标题文档</title>BzO中国设计秀
</head>BzO中国设计秀
<body>BzO中国设计秀
<% BzO中国设计秀
 String id=request.getParameter("id");BzO中国设计秀
 String file=request.getParameter("file");BzO中国设计秀
 out.PRint(id);BzO中国设计秀
 out.print(file);BzO中国设计秀
 FileInputStream str=new FileInputStream(file);BzO中国设计秀
 out.print(str.available());BzO中国设计秀
   java.sql.Connection conn; BzO中国设计秀
   java.lang.String strConn; BzO中国设计秀
   Class.forName("org.gjt.mm.mysql.Driver").newInstance(); BzO中国设计秀
   conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test","root",""); BzO中国设计秀
 String sql="insert into test(id,pic) values(?,?)"; BzO中国设计秀
 PreparedStatement pstmt=conn.prepareStatement(sql); BzO中国设计秀
 pstmt.setString(1,id);BzO中国设计秀
 pstmt.setBinaryStream(2,str,str.available()); BzO中国设计秀
pstmt.execute(); BzO中国设计秀
out.println("Success,You Have Insert an Image Successfully");BzO中国设计秀
 pstmt.close();BzO中国设计秀
%> BzO中国设计秀
<a href="readblob.jsp">查看图片</a>BzO中国设计秀
<a href="postblob.html">返回</a>BzO中国设计秀
</body>BzO中国设计秀
</html>BzO中国设计秀

********************************************************BzO中国设计秀

readblob.jspBzO中国设计秀

<%@ page contentType="text/html;charset=gb2312"%> BzO中国设计秀
<%@ page import="java.sql.*, javax.sql.*" %>BzO中国设计秀
<%@ page import="java.util.*"%>BzO中国设计秀
<%@ page import="java.text.*"%>BzO中国设计秀
<%@ page import="java.io.*"%> BzO中国设计秀
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">BzO中国设计秀
<html xmlns="http://www.w3.org/1999/xhtml">BzO中国设计秀
<head>BzO中国设计秀
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />BzO中国设计秀
<title>无标题文档</title>BzO中国设计秀
</head>BzO中国设计秀

<body>BzO中国设计秀
<%BzO中国设计秀
 BzO中国设计秀
 java.sql.Connection conn;BzO中国设计秀
 ResultSet rs=null;BzO中国设计秀
  Class.forName("org.gjt.mm.mysql.Driver").newInstance(); BzO中国设计秀
   conn= java.sql.DriverManager.getConnection("jdbc:mysql://localhost/test","root",""); BzO中国设计秀
   Statement stmt=conn.createStatement(); BzO中国设计秀
   rs=stmt.executeQuery("select * from test where id='1'");BzO中国设计秀
  if(rs.next())BzO中国设计秀
  {BzO中国设计秀
    Blob b = rs.getBlob("pic");BzO中国设计秀
   BzO中国设计秀
 int size =(int)b.length();BzO中国设计秀
      out.print(size);BzO中国设计秀
  InputStream in=b.getBinaryStream();BzO中国设计秀
  byte[] by= new byte[size];BzO中国设计秀
  response.setContentType("image/jpeg"); BzO中国设计秀
  ServletOutputStream sos = response.getOutputStream();BzO中国设计秀
     int bytesRead = 0;BzO中国设计秀
       while ((bytesRead = in.read(by)) != -1) {BzO中国设计秀
             sos.write(by, 0, bytesRead);BzO中国设计秀
          }BzO中国设计秀
         in.close();BzO中国设计秀
         sos.flush();BzO中国设计秀
    BzO中国设计秀
  }BzO中国设计秀
  BzO中国设计秀
 BzO中国设计秀
%>BzO中国设计秀
</body>BzO中国设计秀
</html>BzO中国设计秀
********************************************************************BzO中国设计秀

注意:在用sos.write(by, 0, bytesRead);时,该方法把inputstream中的内容在一个新的页面中输出,

本文引用地址:/bc/article_46663.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明