时间:2021-07-01 10:21:17 帮助过:22人阅读
Oracle数据库 | MySQL数据库 | |
---|---|---|
长文本类型 | Clob | Text |
二进制类型 | Blob | Blob |
CREATE TABLE T_BigData( Id INT PRIMARY KEY AUTO_INCREMENT, Content LONGTEXT, BitData LONGBLOB );
package com.rk.db.h_longtext; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.rk.db.utils.JDBCUtil; /** * 保存大文本数据类型 ( 写longtext) * @author RK * */ public class Demo01 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; FileReader reader = null; try { String sql = "insert into T_BigData(Content) values(?)"; // 1、连接 conn = JDBCUtil.getConnection(); // 2、pstmt 对象 pstmt = conn.prepareStatement(sql); // 先获取文件路径 String path = Demo01.class.getClassLoader().getResource("ReadMe.txt").getPath(); reader = new FileReader(new File(path)); // 3、设置参数 pstmt.setCharacterStream(1, reader); // 4、执行sql pstmt.executeUpdate(); System.out.println("执行完毕"); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { // 5、关闭 JDBCUtil.closeQuietly(reader); JDBCUtil.closeQuietly(pstmt); JDBCUtil.closeQuietly(conn); } } }
package com.rk.db.h_longtext; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.rk.db.utils.JDBCUtil; /** * 读取大文本数据类型 ( 读longtext) * @author RK * */ public class Demo02 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Reader reader = null; try { String sql = "select * from T_BigData"; // 1、连接 conn = JDBCUtil.getConnection(); // 2、pstmt 对象 pstmt = conn.prepareStatement(sql); // 3、读取 rs = pstmt.executeQuery(); // 4、遍历 while(rs.next()) { // 获取长文本数据, 方式1: //reader = rs.getCharacterStream("content"); // 获取长文本数据, 方式2: System.out.print(rs.getString("content")); System.out.println("========================================="); } System.out.println("执行完毕"); } catch (SQLException e) { e.printStackTrace(); } finally { // 5、关闭 JDBCUtil.closeQuietly(reader); JDBCUtil.closeQuietly(rs); JDBCUtil.closeQuietly(pstmt); JDBCUtil.closeQuietly(conn); } } }
package com.rk.db.h_longtext; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.rk.db.utils.JDBCUtil; /** * 保存二进制数据类型 ( 写longblob) * @author RK * */ public class Demo03 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; InputStream inStream = null; try { String sql = "insert into T_BigData(bitData) values(?)"; // 1、连接 conn = JDBCUtil.getConnection(); // 2、pstmt 对象 pstmt = conn.prepareStatement(sql); // 先获取文件路径 inStream = Demo03.class.getClassLoader().getResourceAsStream("dedu_by_sara.mp3"); // 3、设置参数 pstmt.setBinaryStream(1, inStream); // 4、执行sql pstmt.executeUpdate(); System.out.println("执行完毕"); } catch (SQLException e) { e.printStackTrace(); } finally { // 5、关闭 JDBCUtil.closeQuietly(inStream); JDBCUtil.closeQuietly(pstmt); JDBCUtil.closeQuietly(conn); } } }
package com.rk.db.h_longtext; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.rk.db.utils.JDBCUtil; /** * 读取二进制数据类型 ( 读longblob) * @author RK * */ public class Demo04 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; InputStream inStream = null; OutputStream outStream = null; try { String sql = "select * from T_BigData where Id=?"; // 1、连接 conn = JDBCUtil.getConnection(); // 2、pstmt 对象 pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 3); // 3、读取 rs = pstmt.executeQuery(); // 4、遍历 if(rs.next()) { inStream = rs.getBinaryStream("bitData"); File file = new File("D:/myResult.mp3"); outStream = new FileOutputStream(file); copyStream(inStream, outStream); } System.out.println("执行完毕"); } catch (Exception e) { e.printStackTrace(); } finally { // 5、关闭 JDBCUtil.closeQuietly(inStream); JDBCUtil.closeQuietly(outStream); JDBCUtil.closeQuietly(rs); JDBCUtil.closeQuietly(pstmt); JDBCUtil.closeQuietly(conn); } } private static void copyStream(InputStream inStream,OutputStream outStream) throws IOException { byte[] buf = new byte[1024]; int len = 0; while((len = inStream.read(buf, 0, buf.length)) != -1) { outStream.write(buf, 0, len); } } }
JDBC系列:(8)JDBC中大文本类型的处理
标签:text blob jdbc