国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

mysql操作blob經(jīng)驗談

2019-11-18 16:38:31
字體:
供稿:網(wǎng)友

背景!jsp+MySQL 記住 要用mysql的longblob類型來存默認的blob大小不夠

數(shù)據(jù)庫字段:id (char) pic (longblob)

轉(zhuǎn)載請注明出處,這時我與我的知己的合作的結(jié)過

原來操作blob字段時都要先差個空值,在查blob,好麻煩,用prepareStatment就不用那么麻煩了,哈哈

postblob.heml頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<form action="testblob.jsp" method="post" >
<table width="291" border="1">
  <tr>
    <td width="107">id </td>
    <td width="168"><input name="id" type="text" /></td>
  </tr>
  <tr>
    <td>file</td>
    <td><input name="file" type="file" /></td>
  </tr>
  <tr>
    <td><input  type="submit"  value="提交"/></td>
  
  </tr>
</table>
</form>
</body>
</html>
***************************************************************

testblob.jsp

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

********************************************************

readblob.jsp

<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>

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

注意:在用sos.write(by, 0, bytesRead);時,該方法把inputstream中的內(nèi)容在一個新的頁面中輸出,

如果本頁中還有別的內(nèi)容要輸出的話,只有把上述方法改為,bytesRead = in.read(by)) ;

再用out.print(new String(by));方法輸出結(jié)果,注意在這里不能用by.toString()方法,該方法返回的是要輸出內(nèi)容的內(nèi)存地址。mysql中有blob textarea類型大小了66536基本上放點小的東東就足夠了,哈哈,但是現(xiàn)在的數(shù)碼pic越來越大就只能用longblob了 大小有4g能 ,放個電影都夠了哈哈


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汤原县| 多伦县| 都匀市| 张家港市| 永宁县| 喀什市| 铁岭市| 宜州市| 宜丰县| 开阳县| 岐山县| 克拉玛依市| 哈巴河县| 洛南县| 江陵县| 长沙县| 新巴尔虎左旗| 黄平县| 宁化县| 泽普县| 额济纳旗| 凤冈县| 波密县| 沽源县| 尉氏县| 元阳县| 巍山| 甘南县| 辽中县| 宜良县| 平利县| 淳安县| 阿拉尔市| SHOW| 南川市| 忻州市| 沂南县| 叙永县| 鱼台县| 叙永县| 湘潭县|