自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL操作blob 的實際操作經驗匯總

數(shù)據庫 MySQL
我們今天主要向大家講述的是MySQL操作blob 的實際操作經驗,以及對其實際應用代碼的描述,望大家在瀏覽那之后會對其有所收獲。

此文章主要向大家講述的是MySQL操作blob 的實際操作經驗,我們大家都知道jsp(SUN企業(yè)級應用的***)+MySQL(和PHP搭配之***組合) 記住,一定要用MySQL(和PHP搭配之***組合)的longblob類型來存默認的blob大小不夠。

數(shù)據庫字段:id?。ǎ悖瑁幔颍。穑椋恪。╨ongblob)

轉載請注明出處,這時我與我的知己的合作的結過

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

postblob.heml頁面

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xml(標準化越來越近了)ns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>無標題文檔</title> 
  6. </head> 
  7. <body> 
  8. <form action="testblob.jsp(SUN企業(yè)級應用的***)" method="post" > 
  9. <table width="291" border="1"> 
  10. <tr> 
  11. <td width="107">id </td> 
  12. <td width="168"><input name="id" type="text" /></td> 
  13. </tr> 
  14. <tr> 
  15. <td>file</td> 
  16. <td><input name="file" type="file" /></td> 
  17. </tr> 
  18. <tr> 
  19. <td><input type="submit" value="提交"/></td> 
  20. </tr> 
  21. </table> 
  22. </form> 
  23. </body> 
  24. </html> 
  25. testblob.jsp(SUN企業(yè)級應用的***)  
  26. <%@ page contentType="text/html;charset=gb2312"%>   
  27. <%@ page import="java.sql.*" %> 
  28. <%@ page import="java.util.*"%> 
  29. <%@ page import="java.text.*"%> 
  30. <%@ page import="java.io.*"%>   
  31. <html xml(標準化越來越近了)ns="http://www.w3.org/1999/xhtml"> 
  32. <head> 
  33. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  34. <title>無標題文檔</title> 
  35. </head> 
  36. <body> 
  37. <%   
  38. String id=request.getParameter("id");  
  39. String file=request.getParameter("file");  
  40. out.print(id);  
  41. out.print(file);  
  42. FileInputStream str=new FileInputStream(file);  
  43. out.print(str.available());  
  44. java.sql.Connection conn;   
  45. java.lang.String strConn;   
  46. Class.forName("org.gjt.mm.MySQL(和PHP搭配之***組合).Driver").newInstance();   
  47. connjava.sql.DriverManager.getConnection("jdbc:MySQL(和PHP搭配之***組合)://localhost/test","root","");   
  48. String sql="insert into test(id,pic) values(?,?)";   
  49. PreparedStatement pstmt=conn.prepareStatement(sql);   
  50. pstmt.setString(1,id);  
  51. pstmt.setBinaryStream(2,str,str.available());   
  52. pstmt.execute();   
  53. out.println("Success,You Have Insert an Image Successfully");  
  54. pstmt.close();  
  55. %>   
  56. <a href="readblob.jsp(SUN企業(yè)級應用的***)">查看圖片</a> 
  57. <a href="postblob.html">返回</a> 
  58. </body> 
  59. </html> 
  60. readblob.jsp(SUN企業(yè)級應用的***)  
  61. <%@ page contentType="text/html;charset=gb2312"%>   
  62. <%@ page import="java.sql.*, javax.sql.*" %> 
  63. <%@ page import="java.util.*"%> 
  64. <%@ page import="java.text.*"%> 
  65. <%@ page import="java.io.*"%>   
  66. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  67. <html xml(標準化越來越近了)ns="http://www.w3.org/1999/xhtml"> 
  68. <head> 
  69. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  70. <title>無標題文檔</title> 
  71. </head> 
  72. <body> 
  73. <%  
  74. java.sql.Connection conn;  
  75. ResultSet rs=null;  
  76. Class.forName("org.gjt.mm.MySQL(和PHP搭配之***組合).Driver").newInstance();   
  77. connjava.sql.DriverManager.getConnection("jdbc:MySQL(和PHP搭配之***組合)://localhost/test","root","");   
  78. Statement stmt=conn.createStatement();   
  79. rs=stmt.executeQuery("select * from test where id='1'");  
  80. if(rs.next())  
  81. {  
  82. Blob b = rs.getBlob("pic");  
  83. int size =(int)b.length();  
  84. out.print(size);  
  85. InputStream in=b.getBinaryStream();  
  86. byte[] bynew byte[size];  
  87. response.setContentType("image/jpeg");   
  88. ServletOutputStream sos = response.getOutputStream();  
  89. int bytesRead = 0;  
  90. while ((bytesRead = in.read(by)) != -1) {  
  91. sos.write(by, 0, bytesRead);  
  92. }  
  93. in.close();  
  94. sos.flush();  
  95. }  
  96. %> 
  97. </body> 
  98. </html> 

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

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

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

以上的相關內容就是對MySQL操作blob經驗談的介紹,望你能有所收獲。

【編輯推薦】

  1. MySQL AUTO_INCREMENT的正確用法
  2. MySQL數(shù)據庫在linux下遠程的連接錯誤
  3. MySQL分頁查詢通用存儲過程的代碼總結
  4. MySQL數(shù)據庫中MySQL_real_connect的基本設置
  5. 在Windows環(huán)境下的MySQL數(shù)據庫,精彩比賽

 

責任編輯:佚名 來源: 博客園
相關推薦

2010-05-20 17:40:54

MySQL編碼

2010-06-11 10:30:38

MySQL編碼

2010-06-10 09:54:54

MySQL編碼

2010-06-07 16:36:29

MySQL連接字符串

2010-05-18 17:39:13

MySQL alter

2010-05-14 10:57:43

MySQL操作blob

2009-10-14 10:49:38

綜合布線系統(tǒng)

2010-05-12 13:45:25

Mysql 復制設置

2010-05-17 11:26:49

MySQL 多級同步

2010-05-25 16:31:23

MySQL配置

2010-05-26 16:53:21

MySQL show

2010-05-19 10:37:06

MySQL expla

2010-03-12 15:29:19

Pythonexe

2010-05-28 18:16:43

MySQL 操作日志

2010-05-18 10:07:01

MySQL命令

2010-05-12 11:27:01

MySQL SQL

2010-05-27 17:35:36

MYSQL DELET

2010-05-27 16:12:10

MySQL索引

2010-05-18 09:31:05

MySQL修改表

2010-05-26 14:55:43

MySQL存儲過程
點贊
收藏

51CTO技術棧公眾號