JDBC可滚动可更新感知更新结果集
2009-12-19 00:00:00 来源:WEB开发网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) not null,
pswd varchar(50) default null,
pic longblob,
remark longtext,
primary key (id)
);
三、插入读取blob
import lavasoft.common.DBToolkit;
import java.io.*;
import java.sql.*;
/**
* 操作MySQL5的blob字段
*
* @author leizhimin 2009-12-3 11:34:50
*/
public class BlobTest {
public static void main(String[] args) {
insertBlob();
queryBlob();
}
public static void insertBlob() {
Connection conn = DBToolkit.getConnection();
PreparedStatement ps = null;
try {
String sql = "insert into testdb.user (name, pswd, pic) values (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "zhangsan");
ps.setString(2, "111");
//设置二进制参数
File file = new File("D:\\new\\dbtools\\src\\res\\PIC.PNG");
InputStream in = new BufferedInputStream(new FileInputStream(file));
ps.setBinaryStream(3, in, (int) file.length());
ps.executeUpdate();
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
public static void queryBlob() {
Connection conn = DBToolkit.getConnection();
PreparedStatement ps = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sql = "select pic from user where id = 24";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
InputStream in = rs.getBinaryStream(1);
File file = new File("D:\\new\\dbtools\\src\\res\\PIC_COPY.PNG");
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();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
}
更多精彩
赞助商链接