JDBC 复习2 存取mysql 大数据
大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据
mysql的大数据分为2种 blob 和 text ,没有clob;
对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:
TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT
TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package dbex.mysql; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; import java.net.URL; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import dbex.DBUtil; /** * * @ClassName: MySqlText * @Description: 使用jdbc操作MySQL的大文本 * @author penny * @date 2017年11月27日 下午3:52:32 * */ public class MySqlText { public void doInsert() throws Exception{ // File txt = new File("../../big.txt"); URL path = MySqlText.class.getResource("../../big.txt"); File txt = new File(path.toExternalForm().substring(5)); // System.out.println(); Reader br = new FileReader(txt); OutputStream fw = new PrintStream(System.out); Connection con = DBUtil.getConnection(); PreparedStatement ppst = con.prepareStatement("insert into ex_txt(context) values(?)"); // int len =0; // byte[] buff= new byte[1024]; // StringBuffer sb = new StringBuffer(); // while((len=fr.read(buff))>-1){ // ((PrintStream)fw).print(sb.append(new String(buff)));//成功打印 // } ppst.setCharacterStream(1, br, (int)txt.length()); int row = ppst.executeUpdate(); DBUtil.closeAll(con, ppst, null); System.out.println(row); } void doQuery() throws Exception{ Connection conn = DBUtil.getConnection(); PreparedStatement ppst = conn.prepareStatement("select t.context from ex_txt t where id=2"); ResultSet rs= ppst.executeQuery(); if(rs.next()){ // rs.getString(1); Reader reader = rs.getCharacterStream(1); int len =0; char[] buff = new char[1024]; while((len=reader.read(buff))>-1){ System.out.println(new String(buff)+len); } } DBUtil.closeAll(conn, ppst, rs); } public static void main(String[] args) throws Exception { // new MySqlText().doInsert(); new MySqlText().doQuery(); } }
专注于JavaWeb