歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> MySQL blob字段存儲圖片操作示例

MySQL blob字段存儲圖片操作示例

日期:2017/3/1 10:19:22   编辑:Linux編程

表結構:

  1. create table view(id int unsigned NOT NULL AUTO_INCREMENT, catid int,title varchar(256),picture MEDIUMBLOB, content TEXT,PRIMARY KEY (id));
java類操作:
  1. import java.awt.Image;
  2. import java.io.*;
  3. import java.nio.ByteBuffer;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import javax.swing.ImageIcon;
  10. import org.bean.View;
  11. /**
  12. *
  13. * @author weijian.zhongwj
  14. */
  15. public class ViewPointDao {
  16. public static View getView(Integer catId) {
  17. View view = new View();
  18. try {
  19. String sql2 = "SELECT title, content, picture FROM view where catid=? limit 1";
  20. PreparedStatement stmt2 = BaseDaoFactory.getInstance().prepareStatement(sql2);
  21. stmt2.setInt(1, catId);
  22. ResultSet resultSet = stmt2.executeQuery();
  23. while (resultSet.next()) {
  24. String name = resultSet.getString(1);
  25. String description = resultSet.getString(2);
  26. ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
  27. byte[] buffer = new byte[1];
  28. InputStream is = resultSet.getBinaryStream(3);
  29. while (is != null && is.read(buffer) > 0) {
  30. bb.put(buffer);
  31. }
  32. ImageIcon icon = new ImageIcon(bb.array());
  33. view.setImage(icon.getImage());
  34. view.setTitle(name);
  35. view.setContent(description);
  36. return view;
  37. }
  38. } catch (IOException ex) {
  39. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  40. } catch (SQLException ex) {
  41. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  42. }
  43. return null;
  44. }
  45. public static boolean addView(View view) {
  46. FileInputStream fis = null;
  47. try {
  48. if (exit(view.getCatId())) {
  49. return update(view);
  50. }
  51. String sql = "INSERT INTO view (title, content, catid, picture) VALUES (?, ?, ?, ?)";
  52. PreparedStatement stmt = BaseDaoFactory.getInstance().prepareStatement(sql);
  53. stmt.setString(1, view.getTitle());
  54. stmt.setString(2, view.getContent());
  55. stmt.setInt(3, view.getCatId());
  56. if (view.getImageFile() != null) {
  57. File image = new File(view.getImageFile());
  58. fis = new FileInputStream(image);
  59. //image.length(),返回文件的大小
  60. stmt.setBinaryStream(4, fis, (int) image.length());
  61. } else {
  62. stmt.setBinaryStream(4, null, 0);
  63. }
  64. int count = stmt.executeUpdate();
  65. if (count > 0) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. } catch (IOException ex) {
  71. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  72. } catch (SQLException ex) {
  73. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  74. } finally {
  75. try {
  76. if (fis != null) {
  77. fis.close();
  78. }
  79. } catch (IOException ex) {
  80. }
  81. }
  82. return false;
  83. }
  84. public static boolean update(View view) {
  85. FileInputStream fis = null;
  86. try {
  87. String sql = "update view set title= ? ,content= ? " + (view.getImageFile() != null ? (",picture= ? ") : " ") + "where catid= ? ";
  88. PreparedStatement stmt = BaseDaoFactory.getInstance().prepareStatement(sql);
  89. stmt.setString(1, view.getTitle());
  90. stmt.setString(2, view.getContent());
  91. if (view.getImageFile() != null) {
  92. stmt.setInt(4, view.getCatId());
  93. File image = new File(view.getImageFile());
  94. fis = new FileInputStream(image);
  95. //image.length(),返回文件的大小
  96. stmt.setBinaryStream(3, fis, (int) image.length());
  97. } else {
  98. stmt.setInt(3, view.getCatId());
  99. }
  100. int count = stmt.executeUpdate();
  101. if (count > 0) {
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. } catch (IOException ex) {
  107. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  108. } catch (SQLException ex) {
  109. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  110. } finally {
  111. try {
  112. if (fis != null) {
  113. fis.close();
  114. }
  115. } catch (IOException ex) {
  116. }
  117. }
  118. return false;
  119. }
  120. public static boolean exit(Integer catId) {
  121. try {
  122. String sql2 = "SELECT title, content, picture FROM view where catid=? limit 1";
  123. PreparedStatement stmt2 = BaseDaoFactory.getInstance().prepareStatement(sql2);
  124. stmt2.setInt(1, catId);
  125. ResultSet resultSet = stmt2.executeQuery();
  126. while (resultSet.next()) {
  127. return true;
  128. }
  129. } catch (SQLException ex) {
  130. Logger.getLogger(ViewPointDao.class.getName()).log(Level.SEVERE, null, ex);
  131. }
  132. return false;
  133. }
  134. }
bean:
  1. import java.awt.Image;
  2. public class View {
  3. /**
  4. * 景點標題
  5. */
  6. private String title;
  7. /**
  8. * 景點內容
  9. */
  10. private String content;
  11. /**
  12. * 景點圖片
  13. */
  14. private Image image;
  15. /**
  16. * 景點圖片上傳路徑
  17. */
  18. private String imageFile;
  19. /**
  20. * 分類id
  21. */
  22. private int catId;
  23. public int getCatId() {
  24. return catId;
  25. }
  26. public void setCatId(int catId) {
  27. this.catId = catId;
  28. }
  29. public String getContent() {
  30. return content;
  31. }
  32. public void setContent(String content) {
  33. this.content = content;
  34. }
  35. public Image getImage() {
  36. return image;
  37. }
  38. public void setImage(Image image) {
  39. this.image = image;
  40. }
  41. public String getTitle() {
  42. return title;
  43. }
  44. public void setTitle(String title) {
  45. this.title = title;
  46. }
  47. public String getImageFile() {
  48. return imageFile;
  49. }
  50. public void setImageFile(String imageFile) {
  51. this.imageFile = imageFile;
  52. }
  53. }
Copyright © Linux教程網 All Rights Reserved