How to store text and pictures in MySQL

How to store text and pictures in MySQL

Large Text Data Types in Oracle

Clob long text type (not supported in MySQL, text is used instead)
Blob binary type

MySQL database

Text long text type TINYTEXT: 256 bytes
  TEXT: 65,535 bytes => ~64kb
  MEDIUMTEXT: 16,777,215 bytes => ~16MB
  LONGTEXT: 4,294,967,295 bytes => ~4GB
Blob binary type

For example:

Create a table

CREATE TABLE test(
   id INT PRIMARY KEY AUTO_INCREMENT,
   content LONGTEXT, -- text field img LONGBLOB -- picture field);

When storing text, it is stored in character type, and when storing pictures, it is stored in binary type. The specific method of setting parameters is different from the method of obtaining data.

For example:

// When storing text, set the parameter to character stream FileReader reader
pstmt.setCharacterStream(1, reader);
// When getting parameters // Method 1:
Reader r = rs.getCharacterStream("content");
// Get long text data, method 2:
System.out.print(rs.getString("content"));
// When storing binary images // Set the parameter to the binary stream InputStream in 
pstmt.setBinaryStream(1, in);
// Get the binary stream InputStream in = rs.getAsciiStream("img");
/**
 * Save Photo* 
 */
@Test
public void test2(){
  String sql = "insert into test(img) values(?)";
  try{
    con = JDBCUtil.getConnection();
    pstmt = con.prepareStatement(sql);
    // Set parameters // Get text File file = new File("f:/a.jpg");
    InputStream in = new FileInputStream(file);
    // Set the parameter to binary stream pstmt.setBinaryStream(1, in);
    // Execute 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();
    }
  }
}
/**
 * Get photos * 
 */
@Test
public void test3(){
  String sql = "select * from test where id=?;";
  try{
    con = JDBCUtil.getConnection();
    pstmt = con.prepareStatement(sql);
    // Set parameters pstmt.setInt(1, 2);
    // Execute query 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();
    }
  }
}

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • MySQL implements a solution similar to Oracle sequence
  • mysql code to implement sequence function
  • Can't connect to local MySQL through socket ''/tmp/mysql.sock'' solution
  • A complete list of commonly used MySQL functions (classified and summarized)
  • Use MySQL master-slave configuration to achieve read-write separation and reduce database pressure
  • mysql+spring+mybatis to realize code configuration of database read-write separation
  • How to completely delete the MySQL service (clean the registry)
  • Several ways to store images in MySQL database
  • Installation and use of mysql on Ubuntu (general version)
  • Combining insert and select to implement the method of "inserting the maximum value of a field in the database + 1"

<<:  Reasons and solutions for not being able to detect array changes in Vue2

>>:  How to create a virtual environment using virtualenv under Windows (two ways)

Recommend

Mini Program Custom TabBar Component Encapsulation

This article example shares the specific code for...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

Native js to realize a simple snake game

This article shares the specific code of js to im...

Tips for making web table frames

<br />Tips for making web table frames. ----...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

An article to understand Linux disks and disk partitions

Preface All hardware devices in the Linux system ...

Detailed steps to install the specified version of docker (1.12.6) using rpm

1. Reasons If the system is Centos7.3, the Docker...

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

Detailed explanation of docker version es, milvus, minio startup commands

1. es startup command: docker run -itd -e TAKE_FI...

Deep understanding of the use of ::before/:before and ::after/:after

Part 1: Basics 1. Unlike pseudo-classes such as :...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

Web developers are concerned about the coexistence of IE7 and IE8

I installed IE8 today. When I went to the Microso...

Introduction and analysis of three Binlog formats in MySQL

one. Mysql Binlog format introduction Mysql binlo...