CLOB 以流的方式处理存储大量的文本数据
tinytext 最大长度255
text最大长度65535
mediumtext 最大长度 (2^24)-1
longtext最大长度4GB
public class Demo05 {
/*测试文本大对象*/
public static void main(String[] args) throws SQLException {
Connection conn= null;
PreparedStatement ps1 = null;
Reader r=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
conn= DriverManager.getConnection(url,"root","xjh1999");//建立连接
ps1=conn.prepareStatement("insert into test (姓名,自我介绍) values(?,?)");
ps1.setString(1,"四毛");
ps1.setClob(2,new FileReader(new File("D:\\test\\四毛\\四毛2.txt")));//通过reader传入文件文本
//如果想传入字符串则 ps1.setClob(2,new BufferedReader(new InputStreamReader(new ByteArrayInputStream("我和马曈曈".getBytes()))));
ps1.executeUpdate();
//若想读取文本数据,也使用流操作:
ps1=conn.prepareStatement("select * from test where 姓名=?");
ps1.setString(1,"四毛2");
rs=ps1.executeQuery();
while (rs.next()){
Clob c=rs.getClob("自我介绍");
r=c.getCharacterStream();
int temp=0;
while((temp=r.read())!=-1){
System.out.print((char)temp);}
}
//关闭流以及接口
} catch (Exception e) {
e.printStackTrace();
}finally {
if(r!=null){
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps1!=null){
try {
ps1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
BLOB用于存储大量二进制数据
以流的方式处理
tinybolb 最大长度255
blob最大长度65535
mediumblob 最大长度 (2^24)-1
longblob最大长度4GB
核心操作:
ps1=conn.prepareStatement("insert into test (姓名,自我介绍,headimg) values(?,?,?)");
ps1.setString(1,"四毛3");
ps1.setClob(2,new BufferedReader(new InputStreamReader(new ByteArrayInputStream("我和马曈曈".getBytes()))));
ps1.setBlob(3,new FileInputStream(new File("E:\\XIAO\\MYT\\我们的照片\\IMG_9947.JPG")));//将图片按输入流方式存储
ps1.executeUpdate();