认识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