BLOB类型字段
- BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据,插入blob类型的数据,必须使用PreparedStatement,因为BLOB类型数据是不能通过字符串拼接的
- 有四种类型,TinyBlob类型最大字节255 Blob类型最大65k MediumBlob类型最大16M LongBlob类型最大4G
- 但是如果存储的文件过大会影响到性能
TEXT字段L
- 也有四种类型,TinyText 最大256字节,TEXT类型64kb ,MediumTEXT最大16M,LongTEXT类型最大4G
向数据表中插入BLOB数据和TEXT类型数据
package com.blobANDtext;
import com.utils.JDBCUtils;
import com.utils.Read;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @author 承夕
* @date 2020/3/1 0001 - 10:03
* @contact:https://github.com/chengxi0
*/
public class BlobAndTextDemo1
{
public static void main(String[] args) throws IOException {
String sql = "insert into customers (name ,email , birth ,photo,`text`) values(?,?,?,?,?) ;";
FileInputStream fi = new FileInputStream(new File("photos/proxy.jpg"));
InputStreamReader fi2 = new InputStreamReader(new FileInputStream("texts/myself.txt"));
int i = insertBlobText(sql, "承夕", "19834666@qq.com", "1999-05-17", fi,fi2);
System.out.println(i);
fi2.close();
fi.close();
}
public static int insertBlobText(String sql, Object... args) {
Connection conn = null ;
PreparedStatement pstm = null ;
try {
//获取连接
conn = JDBCUtils.getConnection();
//获取PreparedStatement
pstm = conn.prepareStatement(sql);
//填充占位符
for (int i = 0; i < args.length; i++) {
if (args[i].getClass() == InputStreamReader.class) {
pstm.setCharacterStream(i + 1, (InputStreamReader)args[i]);
}else {
pstm.setObject(i + 1, args[i]);
}
}
int i = pstm.executeUpdate();
return i ;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, pstm, null);
}
return 0 ;
}
}
注意点:
笔者在进行数据插入text类型的时候,使用FileInputStream的txt文件在插入数据库时出现了乱码现象,然后改成使用FileInputReader,但是这是的SetObject方法却也出现错误,无奈之下只能使用文档的setCharacter方法插入text.txt文件,请问有声明办法解决这个问题呢。
修改数据表中的Blob字段
package com.blobANDtext;
import com.utils.JDBCUtils;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @author 承夕
* @date 2020/3/1 0001 - 12:53
* @contact:https://github.com/chengxi0
*/
public class BlobAndTextDemo2 {
public static void main(String[] args) throws IOException {
String sql = "update customers set photo = ? where id = ?";
FileInputStream is = new FileInputStream(new File("photos/plane.jpg"));
updateBlobText(sql,is,21);
is.close();
}
public static int updateBlobText(String sql, Object... args) {
Connection conn = null ;
PreparedStatement pstm = null ;
try {
//获取连接
conn = JDBCUtils.getConnection();
//获取PreparedStatement对象
pstm = conn.prepareStatement(sql);
//填充占位符
for (int i = 0; i < args.length; i++) {
pstm.setObject(i + 1, args[i]);
}
int i = pstm.executeUpdate();
return i;
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(conn, pstm, null);
}
return 0;
}
}
从数据表中读取大数据类型
package com.blobANDtext;
import com.utils.JDBCUtils;
import java.io.*;
import java.sql.*;
/**
* @author 承夕
* @date 2020/3/1 0001 - 13:18
* @contact:https://github.com/chengxi0
*/
public class BlobAndTextDemo3 {
public static void main(String[] args) {
String sql = "select photo from customers where id = ?";
int i = readBlobText(sql, 21);
System.out.println(i);
}
public static int readBlobText(String sql ,Object...args) {
Connection conn = null ;
PreparedStatement pstm = null ;
try {
//获取连接
conn = JDBCUtils.getConnection();
//获取PreparedStatement对象
pstm = conn.prepareStatement(sql);
//填充占位符
for (int i = 0; i < args.length; i++) {
pstm.setObject(i + 1, args[i]);
}
ResultSet rs = pstm.executeQuery();
if(rs.next()){
InputStream is = rs.getBinaryStream(1);
//接下就是输入输出流的对接
FileOutputStream fos = new FileOutputStream(new File("downloadPhotos/plane.jpg"));
int len = 0 ;
byte[] brr = new byte[1024*8];
while ((len = is.read(brr) )!= -1) {
fos.write(brr, 0, len);
}
fos.close();
is.close();
}
return 1;
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, pstm, null);
}
return 0 ;
}
}
承夕 发布了34 篇原创文章 · 获赞 4 · 访问量 425 私信 关注