MySQL存储文本和图片的方法

作者:kerer-sk 时间:2024-01-12 17:56:03 

Oracle中大文本数据类型


Clob  长文本类型  (MySQL中不支持,使用的是text)
Blob  二进制类型

MySQL数据库


Text   长文本类型
 TINYTEXT:   256 bytes
 TEXT:     65,535 bytes    => ~64kb
 MEDIUMTEXT:  16,777,215 bytes  => ~16MB
 LONGTEXT:   4,294,967,295 bytes => ~4GB
Blob  二进制类型

例如:

建表


CREATE TABLE test(
  id INT PRIMARY KEY AUTO_INCREMENT,
  content LONGTEXT, -- 文本字段
  img LONGBLOB  -- 图片字段
);

存储文本时是以字符类型存储,存储图片时是以二进制类型存储,具体使用的设置参数方法,和获取数据方法不同。

例如:


// 存储文本时
// 存储时,设置参数为字符流 FileReader reader
pstmt.setCharacterStream(1, reader);
// 获取参数时
// 方式1:
Reader r = rs.getCharacterStream("content");
// 获取长文本数据, 方式2:
System.out.print(rs.getString("content"));
// 存储二进制图片时
// 设置参数为2进制流 InputStream in
pstmt.setBinaryStream(1, in);
// 获取2进制流
InputStream in = rs.getAsciiStream("img");

/**
* 保存照片
*
*/
@Test
public void test2(){
 String sql = "insert into test(img) values(?)";
 try{
   con = JDBCUtil.getConnection();
   pstmt = con.prepareStatement(sql);
   // 设置参数
   // 获取文本
   File file = new File("f:/a.jpg");
   InputStream in = new FileInputStream(file);
   // 设置参数为2进制流
   pstmt.setBinaryStream(1, in);
   // 执行sql
   pstmt.executeUpdate();
   in.close();
 }catch (Exception e) {
   e.printStackTrace();
 }finally{
   try {
     JDBCUtil.close(con, pstmt);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
}
/**
* 获取照片
*
*/
@Test
public void test3(){
 String sql = "select * from test where id=?;";
 try{
   con = JDBCUtil.getConnection();
   pstmt = con.prepareStatement(sql);
   // 设置参数
   pstmt.setInt(1, 2);
   // 执行查询
   rs = pstmt.executeQuery();
   while(rs.next()){
     byte[] buff = new byte[1024];
     InputStream in = rs.getAsciiStream("img");
     int l=0;
     OutputStream out = new FileOutputStream(new File("f:/1.jpg"));
     while((l=in.read(buff))!=-1){
       out.write(buff, 0, l);
     }
     in.close();
     out.close();
   }
 }catch (Exception e) {
   e.printStackTrace();
 }finally{
   try {
     JDBCUtil.close(con, pstmt);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
}

来源:https://blog.csdn.net/u010512964/article/details/59549110

标签:mysql,存储文本,存储图片
0
投稿

猜你喜欢

  • python控制nao机器人身体动作实例详解

    2023-08-26 11:33:17
  • 泛泛而谈界面中的斑马纹设计

    2010-07-15 12:59:00
  • 详细讲解如何为MySQL数据库添加新函数

    2008-11-27 17:06:00
  • PDO::setAttribute讲解

    2023-06-05 18:04:23
  • 如何运行Python程序的方法

    2023-01-13 07:56:03
  • MySQL性能优化之如何高效正确的使用索引

    2024-01-25 11:51:32
  • Java并发编程数据库与缓存数据一致性方案解析

    2024-01-21 21:09:47
  • NumPy.npy与pandas DataFrame的实例讲解

    2022-11-18 07:39:00
  • js实现固定显示区域内自动缩放图片的方法

    2024-04-16 09:51:35
  • Python环境下安装使用异步任务队列包Celery的基础教程

    2023-08-23 23:06:48
  • python深度学习之多标签分类器及pytorch实现源码

    2022-09-26 01:09:12
  • JS+CSS实现仿雅虎另类滑动门切换效果

    2024-04-17 10:34:55
  • Django中模版的子目录与include标签的使用方法

    2022-07-13 03:58:40
  • Django migrations 默认目录修改的方法教程

    2021-10-15 20:17:59
  • 详解Python中的Dict(下篇)

    2021-11-10 17:16:14
  • Python Tensor FLow简单使用方法实例详解

    2022-01-01 16:55:44
  • js里面的变量范围分享

    2024-04-19 09:51:47
  • Python中非常实用的Math模块函数教程详解

    2022-12-08 02:12:49
  • Python-opencv实现红绿两色识别操作

    2021-05-04 18:35:51
  • Mybatis出现ORA-00911: invalid character的解决办法

    2024-01-19 02:41:21
  • asp之家 网络编程 m.aspxhome.com