设为首页 收藏本站
查看: 598|回复: 0

[经验分享] JDBC操作MySQL Lob字段记实

[复制链接]

尚未签到

发表于 2018-9-27 13:23:56 | 显示全部楼层 |阅读模式
package lob;  import java.sql.*;
  import java.io.*;
  /**
  * JDBC 读取MySQL lob字段测试
  * File: TestLob4MySQL.java
  * User: leizhimin
  * Date: 2008-3-3 14:44:30
  */

  public>  public static final String url = "jdbc:mysql://localhost/testdb";
  public static final String username = "root";
  public static final String password = "leizhimin";
  public static final String driverClassName = "com.mysql.jdbc.Driver";
  /**
  * 数据库连接获取器
  *
  * @return 数据库连接
  */
  public static Connection makeConnection() {
  Connection conn = null;
  try {
  Class.forName(driverClassName);
  } catch (ClassNotFoundException e) {
  e.printStackTrace();
  }
  try {
  conn = DriverManager.getConnection(url, username, password);
  } catch (SQLException e) {
  e.printStackTrace();
  }
  return conn;
  }
  /**
  * 测试数据库连接
  */
  public static void testConnection() {
  Connection conn = makeConnection();
  try {
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * FROM mysql.user");
  while (rs.next()) {
  String s1 = rs.getString(1);
  System.out.println(s1);
  }
  rs.close();
  stmt.close();
  } catch (SQLException e) {
  e.printStackTrace();
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  /**
  * 插入Lob字段
  */
  public static void testInsertlob() {
  Connection conn = makeConnection();
  try {
  conn.setAutoCommit(false);
  File txtFile = new File("C:\\txt.txt");
  File imgFile = new File("C:\\img.png");
  int txt_len = (int) txtFile.length();
  int img_len = (int) imgFile.length();
  try {
  InputStream fis1 = new FileInputStream(txtFile);
  InputStream fis2 = new FileInputStream(imgFile);
  PreparedStatement pstmt = conn.prepareStatement("INSERT INTO T_LOB(NAME,TXT,IMG) VALUES('G',?,?)");
  pstmt.setAsciiStream(1, fis1, txt_len);
  pstmt.setBinaryStream(2, fis2, img_len);
  pstmt.executeUpdate();
  conn.commit();
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  }
  } catch (SQLException e) {
  e.printStackTrace();
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  /**
  * 读取lob字段
  */
  public static void testQueryLob() {
  Connection conn = makeConnection();
  try {
  conn.setAutoCommit(false);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT TXT,IMG FROM T_LOB");
  int i = 1;
  while (rs.next()) {
  Clob clob = rs.getClob("TXT");
  Blob blob = rs.getBlob("IMG");
  InputStream txtIs = rs.getAsciiStream("TXT");
  InputStream imgIs = rs.getBinaryStream("IMG");
  InputStreamReader txtIsr = new InputStreamReader(txtIs);
  InputStreamReader imgIsr = new InputStreamReader(imgIs);
  BufferedReader buff_txtIsr = new BufferedReader(txtIsr);
  BufferedReader buff_imgIsr = new BufferedReader(imgIsr);
  String line = null;
  while (null != (line = buff_txtIsr.readLine())) {
  System.out.println(line); //将其输出至屏幕,实际你可以按照需要处理
  }
  File fileOutput = new File("c:\\img_x" + i + ".png");
  FileOutputStream fo = new FileOutputStream(fileOutput);
  int c;
  while ((c = imgIs.read()) != -1)
  fo.write(c);
  fo.close();
  System.out.println("img " + i + " retrieved!");
  i++;
  }
  conn.commit();
  } catch (SQLException e) {
  e.printStackTrace();
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  /**
  * 读取lob字段
  */
  public static void testQueryLob1() {
  Connection conn = makeConnection();
  try {
  conn.setAutoCommit(false);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT TXT,IMG FROM T_LOB");
  while (rs.next()) {
  Clob clob = rs.getClob("TXT");
  Blob blob = rs.getBlob("IMG");
  InputStream txtIs = clob.getAsciiStream();
  InputStream imgIs = blob.getBinaryStream();
  InputStreamReader txtIsr = new InputStreamReader(txtIs);
  InputStreamReader imgIsr = new InputStreamReader(imgIs);
  BufferedReader buff_txtIsr = new BufferedReader(txtIsr);
  BufferedReader buff_imgIsr = new BufferedReader(imgIsr);
  String line = null;
  while (null != (line = buff_txtIsr.readLine())) {
  System.out.println(line); //将其输出至屏幕,实际你可以按照需要处理
  }
  }
  conn.commit();
  } catch (SQLException e) {
  e.printStackTrace();
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  /**
  * 删除lob字段
  */
  public static void testDeleteLob() {
  Connection conn = makeConnection();
  try {
  conn.setAutoCommit(false);
  Statement stmt = conn.createStatement();
  int row = stmt.executeUpdate("DELETE FROM T_LOB");
  conn.commit();
  System.out.println("删除 " + row + " 行数据!");
  } catch (SQLException e) {
  e.printStackTrace();
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  /**
  * 读取lob字段
  */
  public static void testUpdateLob() {
  Connection conn = makeConnection();
  try {
  String in_str="HAHAHAHAHAHA!!!";
  File in_file=new File("c:\\img_haha.png");
  InputStream txt_is = string2InputStream(in_str);
  InputStream img_is =new FileInputStream(in_file);
  conn.setAutoCommit(false);
  PreparedStatement pstmt = conn.prepareStatement("UPDATE T_LOB SET TXT=?, IMG=? WHERE NAME='G'");
  pstmt.setAsciiStream(1,txt_is,in_str.getBytes().length);
  pstmt.setBinaryStream(2,img_is,(int)in_file.length());
  int row = pstmt.executeUpdate();
  conn.commit();
  txt_is.close();
  img_is.close();
  //            System.out.println("更新 " + row + " 行数据!");
  } catch (SQLException e) {
  e.printStackTrace();
  } catch (FileNotFoundException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  } catch (IOException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  } finally {
  try {
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }
  public static void main(String args[]) {
  //        testInsertlob();
  //        testQueryLob();
  //        testQueryLob1();
  //        testDeleteLob();
  testUpdateLob();
  }
  public static InputStream string2InputStream(String str) {
  if (str == null) return null;
  return new ByteArrayInputStream(str.getBytes());
  }
  public static String inputStream2String(InputStream is) {
  StringBuffer sb = new StringBuffer();
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  String inputLine;
  try {
  while ((inputLine = br.readLine()) != null) {
  sb.append(inputLine).append("\n");
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
  return sb.toString();
  }
  }


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-602854-1-1.html 上篇帖子: MySQL表分区详解 下篇帖子: mysql M/S配置小记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表