mysql存clob对象_JDBC处理CLOB 和 BLOB大对象

在数据库中:

clob用于存储大量的文本数据

可以使用字符流操作

clob用于存储大量的二进制数据

可以使用字节流操作

以mysql为例 先准备一张表:

CREATE TABLE `t_user2` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`username` varchar(10) DEFAULT NULL,

`myInfo` text,

`headImage` blob,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

0236d6e22cc02a66b4d03446dc010422.png

ClOB 操作:

public static void main(String[] args) {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

BufferedReader br = null;

try {

// 加载驱动类

Class.forName("com.mysql.jdbc.Driver");

// 建立连接

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useSSL=false", "root", "123456");

/*存入

* ps = conn.prepareStatement("insert into t_user2 (username, myInfo) values(?, ?)");

ps.setString(1, "张三");

//ps.setClob(2, new FileReader(new File("f:/temp/最强兵王0--10.txt")));

ps.setClob(2, new BufferedReader(

new InputStreamReader(new ByteArrayInputStream("hello world!".getBytes()))));

ps.executeUpdate();*/

// 取出

ps = conn.prepareStatement("select * from t_user2 where id = ?");

ps.setInt(1, 2);

rs = ps.executeQuery();

while(rs.next()) {

Clob clob = rs.getClob("myInfo");

br = new BufferedReader(clob.getCharacterStream());

String line;

while((line = br.readLine()) != null) {

System.out.println(line);

}

}

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

if(br != null) {

br.close();

}

} catch (IOException e) {

e.printStackTrace();

}

try {

if(rs != null) {

rs.close();

}

} catch (SQLException 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();

}

}

}

运行代码:

b3650dedb446708268bba922ce240f6f.png

可以看到我把存入的小说又读出来了

blob操作

public static void main(String[] args) {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

BufferedReader br = null;

ByteArrayInputStream bis = null;

FileOutputStream fos = null;

try {

// 加载驱动类

Class.forName("com.mysql.jdbc.Driver");

// 建立连接

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useSSL=false", "root", "123456");

//存入

/*ps = conn.prepareStatement("insert into t_user2 (username, headImage) values(?, ?)");

ps.setString(1, "张飞");

ps.setBlob(2, new BufferedInputStream(new FileInputStream("f:/aa/temp/张飞.jpg")));

ps.executeUpdate();*/

// 取出

ps = conn.prepareStatement("select * from t_user2 where id = ?");

ps.setInt(1, 3);

rs = ps.executeQuery();

while(rs.next()) {

Blob blob = rs.getBlob("headImage");

bis = (ByteArrayInputStream) blob.getBinaryStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] flush = new byte[1024];

int len;

while((len = bis.read(flush)) != -1) {

bos.write(flush, 0, len);

}

fos = new FileOutputStream("f:/aa/a.jpg");

fos.write(bos.toByteArray());

fos.flush();

}

} catch (Exception e) {

e.printStackTrace();

}finally {

try {

if(fos != null) {

fos.close();

}

} catch (IOException e) {

e.printStackTrace();

}

try {

if(br != null) {

br.close();

}

} catch (IOException e) {

e.printStackTrace();

}

try {

if(rs != null) {

rs.close();

}

} catch (SQLException 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();

}

}

}

运行测试:

{{uploading-image-960855.png(uploading...)}}

存入的图片又回来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值