- 2023-06-09 11:17:54
- 3427 热度
- 0 评论
认识MySQL的大字段类型
BLOB是一个二进制大对象,可以容纳可变数量的数据。有4种BLOB类型:TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同。
有4种TEXT类型:TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT.这些对应4种BLOB类型,有相同的最大长度和存储需求。
BLOB列被视为二进制字符串(字节字符串)。TEXT列被视为非二进制字符串(字符字符串)。
BLOB列没有字符集,并且排序和比较基于列值字节的数值值。TEXT列有一个字符集,并且根据字符集的 校对规则对值进行排序和比较。
在TEXT或BLOB列的存储或检索过程中,不存在大小写转换。
当未运行在严格模式时,如果你为BLOB或TEXT列分配一个超过该列类型的最大长度的值值,值被截取以保证适合。
几种类型的大字段最大长度说明:
TINYBLOB最大长度为255(2^[8]–1)字节的BLOB列。
TINYTEXT最大长度为255(2^[8]–1)字符的TEXT列。
BLOB[(M)]最大长度为65,535(2^[16]–1)字节的BLOB列。可以给出该类型的可选长度M.如果给出,则MySQL将列创建为最小的但足以容纳M字节长的值的BLOB类型。
TEXT[(M)]最大长度为65,535(2^[16]–1)字符的TEXT列。可以给出可选长度M.则MySQL将列创建为最小的但足以容纳M字符长的值的TEXT类型。
MEDIUMBLOB最大长度为16,777,215(2^[24]–1)字节的BLOB列。
MEDIUMTEXT最大长度为16,777,215(2^[24]–1)字符的TEXT列。
LONGBLOB最大长度为4,294,967,295或4GB(2^[32]–1)字节的BLOB列。LONGBLOB列的最大有效(允许的)长度取决于客户端/服务器协议中配置最大包大小和可用的内存。
LONGTEXT最大长度为4,294,967,295或4GB(2^[32]–1)字符的TEXT列。LONGTEXT列的最大有效(允许的)长度取决于客户端/服务器协议中配置最大包大小和可用的内存。
创建表:
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT , `name` varchar(50) DEFAULT NULL , `pswd` varchar(50) DEFAULT NULL , `pic` longblob DEFAULT NULL , `remark` longtext DEFAULT NULL , PRIMARY KEY (`id`) );
读写大字段的Java代码(注意先导入驱动包):
package test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.Statement; public class JdbcBlobTest { public static final String DBDRIVER = "com.mysql.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://localhost:3306/test"; public static final String DBUSER = "root"; public static final String DBPASS = "11111111"; public static void main(String[] args) throws Exception { Connection conn = null; Class.forName(DBDRIVER); conn = (Connection) DriverManager.getConnection(DBURL, DBUSER, DBPASS); System.out.println(conn); saveBlob(conn); // 写入,先准备好要读取的图片 queryBlob(conn); // 读取 conn.close(); } public static void saveBlob(Connection conn) throws Exception { PreparedStatement ps = null; String sql = "insert into user (name, pswd, pic ) values (?, ?, ? )"; ps = (PreparedStatement) conn.prepareStatement(sql); ps.setString(1, "zhangsan"); ps.setString(2, "111"); // 设置二进制参数 File file = new File("D:\\20150421135358.jpg"); InputStream in = new BufferedInputStream(new FileInputStream(file)); ps.setBinaryStream(3, in); // 以下两种方式均可 // ps.setBlob(3, in); // ps.setBytes(3, inputStream2Byte(in)); ps.executeUpdate(); in.close(); } // public static byte[] inputStream2Byte(InputStream inStream) throws Exception { // int count = 0; // while (count == 0) { // count = inStream.available(); // } // byte[] b = new byte[count]; // inStream.read(b); // return b; // } public static void queryBlob(Connection conn) throws Exception { Statement stmt = null; ResultSet rs = null; String sql = "select pic from user where id = 1"; stmt = (Statement) conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { InputStream in = rs.getBinaryStream(1); File file = new File("D:\\20150421135358-2.jpg"); OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); byte[] buff = new byte[1024]; for (int i = 0; (i = in.read(buff)) > 0;) { out.write(buff, 0, i); } out.flush(); out.close(); in.close(); } rs.close(); stmt.close(); } }
OVER
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- discuz(5)