MySQL操作blob的经验研讨

数据库 MySQL
我们今天主要向大家介绍的是MySQL操作blob的经验研讨。以及在MySQL操作blob中一些值得我们注意的事项的描述,以下就是文章的具体内容描述。

以下的文章主要讲述的是MySQL操作blob的经验研讨,如果你在MySQL操作blob的实际操作中有不解之处时,你可以通过以下的文章对其的实际应用与功能有所了解,下面是文章的具体介绍,望你浏览完以下的内容会有所收获。

jsp(SUN企业级应用的首选)+MySQL(和PHP搭配之最佳组合) 记住 要用MySQL(和PHP搭配之最佳组合)的longblob类型来存默认的MySQL操作blob大小不够

数据库字段:id (char) pic (longblob)

转载请注明出处,这时我与我的知己的合作的结过

原来操作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企业级应用的首选)" 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企业级应用的首选)  
  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企业级应用的首选)">查看图片</a> 
  57. <a href="postblob.html">返回</a> 
  58. </body> 
  59. </html> 
  60. readblob.jsp(SUN企业级应用的首选)  
  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搭配之最佳组合)中有MySQL操作blob textarea类型大小了66536基本上放点小的东东就足够了,哈哈,但是现在的数码pic越来越大就只能用longblob了。

【编辑推荐】

  1. 如何通用存储过程来对MySQL分页查询进行操作
  2. MySQL5 master slave安装配置全过程
  3. MySQL高效分页查询的实际操作步骤
  4. MySQL AUTO_INCREMENT实际操作用法
  5. MySQL启动方法与实际操作步骤
责任编辑:佚名 来源: 博客园
相关推荐

2010-06-12 13:39:33

MySQL操作blob

2009-09-04 14:30:28

C#读取BLOB数据

2010-05-20 17:40:54

MySQL编码

2010-06-11 10:30:38

MySQL编码

2009-07-15 16:26:04

iBATIS Clob

2010-06-10 09:54:54

MySQL编码

2010-05-21 15:33:54

MySQL text

2010-06-13 09:09:34

MySQL 4.0.2

2010-05-06 15:04:54

Oracle建立DBL

2009-09-11 13:29:31

LINQ查询操作

2009-09-08 10:57:55

LINQ查询操作

2010-05-19 17:24:55

MySQL编码

2021-12-31 10:32:26

MySQL数据类型

2011-08-08 15:25:42

iPhone SQLite BLOB

2022-02-21 17:28:33

冬奥网络安全

2010-05-07 11:22:18

Unix操作系统

2021-07-26 12:10:37

FacebookMySQL 8.0数据库

2010-04-23 16:00:54

2010-05-21 12:43:06

MySQL 5.0存储
点赞
收藏

51CTO技术栈公众号