jdbc 大数据存储 图片读取
package com.itheima.clob.test; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.Test; import com.itheima.utils.JdbcUtil; /** * 测试大文本数据读写 drop database day16; create database day16; use day16; create table testclob( id int primary key, content longtext ); * @author wangli * */ public class ClobTest { @Test //写入 public void testClobAdd(){ Connection con = null; PreparedStatement st =null; try { con = JdbcUtil.getConnection(); String sql="insert into testclob values(?,?)"; st = con.prepareStatement(sql); //赋值 st.setInt(1, 2); //st.setString(2, "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); File file = new File("src/jdbccfg.properties"); FileReader reader = new FileReader(file);//得到一个文件读取器 st.setCharacterStream(2, reader, (int)file.length());//int jdk5, long jdk1.6+ //MYsql支持的长度有限 ,改成int类型就可以了 //long类型,可以给oracle用 st.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.release(null, st, con); } } @Test //读取 public void testClobReader(){ Connection con = null; PreparedStatement st =null; ResultSet rs = null; try { con = JdbcUtil.getConnection(); String sql="select * from textclob where id=?"; st = con.prepareStatement(sql); //赋值 st.setInt(1, 2); //3.执行查询 rs = st.executeQuery(); if(rs.next()){ //说明结果集中有记录 Reader reader = rs.getCharacterStream("content"); FileWriter fw = new FileWriter("d:/jdbc.properties"); int len=-1; char [] buffer = new char[1024]; while((len=reader.read(buffer))!=-1){ fw.write(buffer, 0, len); } //关闭流 fw.close(); reader.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.release(null, st, con); } } }