注:
一般的视频,图片,和大文本都不会放在数据库中。
都是放在文件服务器上
CLOB文本大对象操作
1、CLOB(CharacterLargeObject)
用于存储大量的文本数据
大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以
流的方式来处理的。而非一般的字段,一次即可读出数据。
2、Mysql中相关类型:
TINYTEXT最大长度为255(2^[8]–1)字符的TEXT列。
TEXT[(M)]最大长度为65,535(2^[16]–1)字符的TEXT列。
MEDIUMTEXT最大长度为16,777,215(2^[24]–1)字符的TEXT列。
LONGTEXT最大长度为4,294,967,295或4GB(2^[32]–1)字符的TEXT列。
public class ClobTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Reader r = null;
try {
//加载驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/crm","root","123456");
ps = conn.prepareStatement("insert into pdman_db_version (version_desc) values (?) ");
String s= "/Users/tangscott/Documents/java/workspace-idea/springboot2web/happy.sxt";
//将文本文件内容直接输入到数据库中
ps.setClob(1, new FileReader(new File(s)));
ps.execute();
//将程序中的字符串输入到数据库的CLOB字段中
ps.setClob(1, new BufferedReader(new InputStreamReader(
new ByteArrayInputStream("aaaabbbbbb".getBytes()))));
ps.execute();
ps = conn.prepareStatement("select * from pdman_db_version ");
rs = ps.executeQuery();
while(rs.next()){
Clob c = rs.getClob("version_desc");
r = c.getCharacterStream();
int temp = 0;
while((temp=r.read())!=-1){
System.out.print((char)temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(r!=null){
r.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
BLOB二进制大对象的使用
1、BLOB(BinaryLargeObject)
用于存储大量的二进制数据(可以存电影)
大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的
方式来处理的。而非一般的字段,一次即可读出数据。
2、Mysql中相关类型:
TINYBLOB最大长度为255(2^[8]–1)字节的BLOB列。
BLOB[(M)]最大长度为65,535(2^[16]–1)字节的BLOB列。
MEDIUMBLOB最大长度为16,777,215(2^[24]–1)字节的BLOB列。
LONGBLOB最大长度为4,294,967,295或4GB(2^[32]–1)字节的BLOB列。、
public class BlobText {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
//加载驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/crm","root","123456");
String img= "/Users/tangscott/Documents/java/workspace-idea/springboot2web/io.png";
ps = conn.prepareStatement("insert into pdman_db_version (head_Img) values (?) ");
ps.setBlob(1, new FileInputStream(img));
ps.execute();
ps = conn.prepareStatement("select * from pdman_db_version ");
rs = ps.executeQuery();
while(rs.next()){
Blob b = rs.getBlob("head_Img");
is = b.getBinaryStream();
String s = "/Users/tangscott/Documents/java/workspace-idea/springboot2web/io-cp.png";
os = new FileOutputStream(s);
int temp = 0;
while((temp=is.read())!=-1){
os.write(temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}