db2 快速清除表中所以的数据
2012-11-01 09:27
176 查看
JDBC对LOB的读写 在JDBC中提供了java.sql.Blob和java.sql.Clob两个类分别代表BLOB和CLOB数据 •BLOB(Binary Large Object):用于存储大量的二进制数据 •CLOB(Character Large Object):用于存储大量的文本数据 本文以MySQL为例,通过最基本的JDBC技术来处理大字段的插入、读取操作。 环境: MySQL5 JDK1.6 1)首先创建一张数据表: create table stuinfo( id int(11) not null auto_increment, name varchar(20)default null, content mediumtext , image blob, primary key (id) ); 2)先数据库中插入BLOB和CLOB类型的数据 package cn.wjz.jdbc; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.wjz.db.ConnectionFactory; import cn.wjz.db.DbClose; public class LobWrite { public void LobW(){ Connection conn = ConnectionFactory.getConnection() ; PreparedStatement pstmt = null ; String sql = "insert into stuinfo(name,content,image) values(?,?,?)" ; try{ pstmt = conn.prepareStatement(sql) ; pstmt.setString(1, "test") ; /从文件中获取输入流———读取文本 File file = new File("D:\\wjz.gif"); try{ InputStreamReader reader = new InputStreamReader (new FileInputStream("D:\\test.txt")); pstmt.setCharacterStream (2, reader, (int) file.length()); }catch(Exception e){ e.printStackTrace() ; } File file1 = new File("D:\\wjz.gif"); InputStream in; try { in = new BufferedInputStream (new FileInputStream(file1)); pstmt.setBinaryStream (3, in, (int) file1.length()); } catch (FileNotFoundException e) { e.printStackTrace(); } if(pstmt.executeUpdate() == 1){ System.out.println("恭喜您成功添加记录!!"); }else{ System.out.println("对不起您添加记录失败!!"); } }catch(SQLException e){ e.printStackTrace(); DbClose.close(pstmt , conn ) ; } } public static void main(String args[]){ LobWrite lobtest = new LobWrite() ; lobtest.LobW() ; } } 3)从MySql数据库中读取BLOB和CLOB类型数据: package cn.wjz.jdbc; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.Reader; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.wjz.db.ConnectionFactory; import cn.wjz.db.DbClose; public class LobRead { public void lobread(){ Connection conn = ConnectionFactory.getConnection(); PreparedStatement pstmt = null; String sql = "SELECT id,name,content,image FROM stuinfo WHERE name=?"; ResultSet rs = null; BufferedReader br =null; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, "test"); rs = pstmt.executeQuery(); while (rs.next()) { Reader rd = rs.getCharacterStream(3); br = new BufferedReader(rd); String str = null; while((str = br.readLine()) != null){ System.out.println(str); } Blob blob = rs.getBlob(4); BufferedInputStream bis = new BufferedInputStream(blob.getBinaryStream()); BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream("d:/wjz.gif")); byte[] buffer = new byte[1024]; int count = -1; while ((count = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, count); } bos.flush(); bos.close(); System.out.println("\n------->图片写好了!"); } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { DbClose.close(pstmt, conn); } } public static void main(String[] args) { LobRead lobread = new LobRead(); lobread.lobread(); } }
在处理blob字段时候,由于直接处理的是二进制流,所以没啥问题。在处理clob字段的时候,由于数据库对clob是以字符的形式进行存储,这就有一个编码问题。本文虽然成功的插入读取了clob字段,但是还没有解决乱码问题,因为JDBC在获取到clob的时候,已经对其进行了编码,Reader reader = rs.getCharacterStream(1); 这就导致了编码的混乱,如果要彻底解决,还需要看看MySQL驱动的实现。通过非常规手段来解决。为了绕开此问题,可以将clob的数据存储为blog来操作,可以避免此问题。
相关文章推荐
- mysql 快速清除指定表中数据的sql语句
- diskpart的clean清除的数据快速恢复方法
- 使用 DB2 的 LOAD FROM CURSOR 特性快速轻松地转移数据 (From IBM developerWorks )
- 用sp_MSforeachtable快速清除数据库表中的数据
- Oracle快速清除表数据
- db2 快速清空表数据
- 快速从db2导出数据到oracle
- DB2快速构建千万级性能测试基础数据
- DB2 Magazine:快速数据访问的捷径
- db2 快速清空表数据
- AndroidStudio技巧:快速清除app数据
- db2 数据恢复-快速
- 使用 DB2 的 LOAD FROM CURSOR 特性快速轻松地转移数据
- linux下快速清除文件内容
- 看我如何快速学习.Net(高可用数据采集平台)
- MySQL误操作后如何快速恢复数据
- bootstrap 弹窗 数据清除
- 用SQLPLUS脚本快速导出大量数据的脚本
- Netty 快速入门系列 - Chapter 7 数据包协议【第十七讲】解决方案- 分割符 及 长度+数据
- GeoServer快速发布地图数据