大数量数据:
字符:
存储字符内容: mysql: char varchar 长度有限的。 65535
大容量的字符字段:
mysql: text longtext(4G字符内容)
oracle : clob longclob字节:
mysql: blob(65kb) mediumblob(16mb) longblog(4GB)
oracle: blob
使用jdbc操作text字段:
public class Demo1 {
public static void main(String[] args) {
//write();
/**
* 读取
*/
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "select * from news where id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 1);
//执行查询
rs = stmt.executeQuery();
//读取结果集
while(rs.next()){
//获取text字段
//1)可以把text字段内容当前字符串读取
/* String content = rs.getString("content");
System.out.println(content);
*/
//2)可以把text字段当做输入流读取
Clob clob = rs.getClob("content");
//通过clob字段读取输入流
Reader reader = clob.getCharacterStream();
//把输入流输出到文件中
FileWriter writer = new FileWriter("e:/abc.txt");
char[] buf = new char[512];
int len = 0;
while( (len=reader.read(buf))!=-1 ){
//边读边写出
writer.write(buf, 0, len);
}
//关闭流
writer.close();
reader.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(stmt, conn);
}
}
private static void write() {
/**
* 写入
*/
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into news(title,content) values(?,?)";
//预编译
stmt = conn.prepareStatement(sql);
//参数赋值
stmt.setString(1, "昨天是父亲节,全国各地都在纷纷庆祝!!!");
/**
* 读取本地的字符文件
*/
Reader reader = new FileReader("e:/url.txt");
//设置text字段
stmt.setClob(2, reader);
//执行
stmt.executeUpdate();
System.out.println("添加成功");
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(stmt, conn);
}
}
}
使用jdbc操作blob字段:
public class Demo2 {
public static void main(String[] args) {
//write();
/**
* 读取
*/
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "select * from news where id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 1);
rs = stmt.executeQuery();
while(rs.next()){
//读取blob字段
Blob blob = rs.getBlob("attachments");
//通过blob字段获取输入字节流
InputStream in = blob.getBinaryStream();
//把输入字节流写出文件中
FileOutputStream out = new FileOutputStream("e:/abc.mp3");
byte[] buf = new byte[1024];
int len = 0;
//边读边写
while( (len=in.read(buf))!=-1 ){
out.write(buf, 0, len);
}
//关闭流
out.close();
in.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(stmt, conn);
}
}
private static void write() {
/**
* 写入
*/
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = JdbcUtil.getConnection();
String sql = "update news set attachments=? where id=?";
//预编译
stmt = conn.prepareStatement(sql);
//参数赋值
/**
* 读取本地字节文件
* 注意:
* 1)发送的数据内容超过了字段的长度限制,则抛出 Data too long...异常,这时需要修改字段的类型
* 2)发送的数据内容超过了1MB(mysql服务器默认的接收数据包的大小),可以到mysql安装目录下的my.ini文件添加一个变量max_allowed_packet=50M即可!
*/
InputStream in = new FileInputStream("e:/02.mp3");
/**
* 设置blob字段
*/
stmt.setBlob(1, in);
stmt.setInt(2, 1);
//执行
stmt.executeUpdate();
System.out.println("添加成功");
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(stmt, conn);
}
}
}