1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.io.File ;
  6. import java.io.FileInputStream ;
  7. import java.io.InputStream ;
  8. public class ClobDemo01{
  9. // 定义MySQL的数据库驱动程序
  10. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  11. // 定义MySQL数据库的连接地址
  12. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  13. // MySQL数据库的连接用户名
  14. public static final String DBUSER = "root" ;
  15. // MySQL数据库的连接密码
  16. public static final String DBPASS = "mysqladmin" ;
  17. public static void main(String args[]) throws Exception{ // 所有异常抛出
  18. Connection conn = null ; // 数据库连接
  19. PreparedStatement pstmt = null ;
  20. String name = "李兴华" ; // 表示姓名
  21. String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;
  22. Class.forName(DBDRIVER) ; // 加载驱动程序
  23. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  24. pstmt = conn.prepareStatement(sql) ; // 创建PreapredStatement对象
  25. File f = new File("d:" + File.separator + "mldn.txt") ;
  26. InputStream input = null ;
  27. input = new FileInputStream(f) ; // 通过输入流读取文件
  28. pstmt.setString(1,name) ;
  29. pstmt.setAsciiStream(2,input,(int)f.length()) ;
  30. pstmt.executeUpdate() ;
  31. conn.close() ; // 数据库关闭
  32. }
  33. };
  1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.sql.ResultSet ;
  6. import java.io.File ;
  7. import java.io.FileInputStream ;
  8. import java.io.InputStream ;
  9. import java.util.Scanner ;
  10. public class ClobDemo02{
  11. // 定义MySQL的数据库驱动程序
  12. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  13. // 定义MySQL数据库的连接地址
  14. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  15. // MySQL数据库的连接用户名
  16. public static final String DBUSER = "root" ;
  17. // MySQL数据库的连接密码
  18. public static final String DBPASS = "mysqladmin" ;
  19. public static void main(String args[]) throws Exception{ // 所有异常抛出
  20. Connection conn = null ; // 数据库连接
  21. PreparedStatement pstmt = null ;
  22. ResultSet rs = null ;
  23. int id = 1 ; // 读取的编号
  24.  
  25. String sql = "SELECT name,note FROM userclob WHERE id=? " ;
  26. Class.forName(DBDRIVER) ; // 加载驱动程序
  27. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  28. pstmt = conn.prepareStatement(sql) ; // 创建PreapredStatement对象
  29. pstmt.setInt(1,id) ;
  30. rs = pstmt.executeQuery() ;
  31. if(rs.next()){
  32. String name = rs.getString(1) ;
  33. StringBuffer note = new StringBuffer() ;
  34. System.out.println("姓名:" + name) ;
  35. InputStream input = rs.getAsciiStream(2) ;
  36. Scanner scan = new Scanner(input) ; // 使用Scanner类读取内容
  37. scan.useDelimiter("\r\n") ; // 将文件换行作为分割符
  38. while(scan.hasNext()){
  39. note.append(scan.next()).append("\n") ;
  40. }
  41. System.out.println("内容:" + note) ;
  42. input.close() ;
  43. }
  44. rs.close() ;
  45. pstmt.close() ;
  46. conn.close() ; // 数据库关闭
  47. }
  48. };
  1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.sql.Clob ;
  6. import java.sql.ResultSet ;
  7. public class ClobDemo03{
  8. // 定义MySQL的数据库驱动程序
  9. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  10. // 定义MySQL数据库的连接地址
  11. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  12. // MySQL数据库的连接用户名
  13. public static final String DBUSER = "root" ;
  14. // MySQL数据库的连接密码
  15. public static final String DBPASS = "mysqladmin" ;
  16. public static void main(String args[]) throws Exception{ // 所有异常抛出
  17. Connection conn = null ; // 数据库连接
  18. PreparedStatement pstmt = null ;
  19. ResultSet rs = null ;
  20. int id = 1 ; // 读取的编号
  21.  
  22. String sql = "SELECT name,note FROM userclob WHERE id=? " ;
  23. Class.forName(DBDRIVER) ; // 加载驱动程序
  24. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  25. pstmt = conn.prepareStatement(sql) ; // 创建PreapredStatement对象
  26. pstmt.setInt(1,id) ;
  27. rs = pstmt.executeQuery() ;
  28. if(rs.next()){
  29. String name = rs.getString(1) ;
  30. System.out.println("姓名:" + name) ;
  31. Clob c = rs.getClob(2) ;
  32. String note = c.getSubString(1,(int)c.length()) ;
  33. System.out.println("内容:" + note ) ;
  34. c.truncate(100) ; // 只能读100个内容
  35. System.out.println("部分读取内容:" + c.getSubString(1,(int)c.length())) ;
  36. }
  37. rs.close() ;
  38. pstmt.close() ;
  39. conn.close() ; // 数据库关闭
  40. }
  41. };
  1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.io.File ;
  6. import java.io.FileInputStream ;
  7. import java.io.InputStream ;
  8. public class BlobDemo01{
  9. // 定义MySQL的数据库驱动程序
  10. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  11. // 定义MySQL数据库的连接地址
  12. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  13. // MySQL数据库的连接用户名
  14. public static final String DBUSER = "root" ;
  15. // MySQL数据库的连接密码
  16. public static final String DBPASS = "mysqladmin" ;
  17. public static void main(String args[]) throws Exception{ // 所有异常抛出
  18. Connection conn = null ; // 数据库连接
  19. PreparedStatement pstmt = null ;
  20. String name = "李兴华" ;
  21. String sql = "INSERT INTO userblob(name,photo) VALUES (?,?) " ;
  22. Class.forName(DBDRIVER) ; // 加载驱动程序
  23. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  24. pstmt = conn.prepareStatement(sql) ;
  25. File f = new File("d:" + File.separator + "mldn.gif") ; // 图片文件
  26. InputStream input = null ;
  27. input = new FileInputStream(f) ;
  28. pstmt.setString(1,name) ; // 设置第一个“?”的内容
  29. pstmt.setBinaryStream(2,input,(int)f.length()) ; // 设置输入流
  30. pstmt.executeUpdate() ; // 更新数据库
  31. pstmt.close() ;
  32. conn.close() ; // 数据库关闭
  33. }
  34. };
  1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.io.File ;
  6. import java.io.FileOutputStream ;
  7. import java.sql.ResultSet ;
  8. import java.io.InputStream ;
  9. import java.io.InputStream ;
  10. import java.io.OutputStream ;
  11. public class BlobDemo02{
  12. // 定义MySQL的数据库驱动程序
  13. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  14. // 定义MySQL数据库的连接地址
  15. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  16. // MySQL数据库的连接用户名
  17. public static final String DBUSER = "root" ;
  18. // MySQL数据库的连接密码
  19. public static final String DBPASS = "mysqladmin" ;
  20. public static void main(String args[]) throws Exception{ // 所有异常抛出
  21. Connection conn = null ; // 数据库连接
  22. PreparedStatement pstmt = null ;
  23. ResultSet rs = null ;
  24. int id = 1 ;
  25. String sql = "SELECT name,photo FROM userblob WHERE id=?" ;
  26. Class.forName(DBDRIVER) ; // 加载驱动程序
  27. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  28. pstmt = conn.prepareStatement(sql) ;
  29. pstmt.setInt(1,id) ;
  30. rs = pstmt.executeQuery() ; // 执行查询
  31. if(rs.next()){
  32. String name = rs.getString(1) ;
  33. System.out.println("姓名:" + name) ;
  34. InputStream input = rs.getBinaryStream(2) ;
  35. File f = new File("d:" + File.separator + "loadmldn.gif") ; // 图片文件
  36. OutputStream out = null ;
  37. out = new FileOutputStream(f) ;
  38. int temp = 0 ;
  39. while((temp=input.read())!=-1){ // 边读边写
  40. out.write(temp) ;
  41. }
  42. input.close() ;
  43. out.close() ;
  44. }
  45. pstmt.close() ;
  46. conn.close() ; // 数据库关闭
  47. }
  48. };
  1. import java.sql.Connection ;
  2. import java.sql.DriverManager ;
  3. import java.sql.SQLException ;
  4. import java.sql.PreparedStatement ;
  5. import java.sql.Blob ;
  6. import java.sql.ResultSet ;
  7. import java.io.File ;
  8. import java.io.FileOutputStream ;
  9. import java.io.InputStream ;
  10. import java.io.InputStream ;
  11. import java.io.OutputStream ;
  12.  
  13. public class BlobDemo03{
  14. // 定义MySQL的数据库驱动程序
  15. public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
  16. // 定义MySQL数据库的连接地址
  17. public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
  18. // MySQL数据库的连接用户名
  19. public static final String DBUSER = "root" ;
  20. // MySQL数据库的连接密码
  21. public static final String DBPASS = "mysqladmin" ;
  22. public static void main(String args[]) throws Exception{ // 所有异常抛出
  23. Connection conn = null ; // 数据库连接
  24. PreparedStatement pstmt = null ;
  25. ResultSet rs = null ;
  26. int id = 1 ;
  27. String sql = "SELECT name,photo FROM userblob WHERE id=?" ;
  28. Class.forName(DBDRIVER) ; // 加载驱动程序
  29. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
  30. pstmt = conn.prepareStatement(sql) ;
  31. pstmt.setInt(1,id) ;
  32. rs = pstmt.executeQuery() ; // 执行查询
  33. if(rs.next()){
  34. String name = rs.getString(1) ;
  35. System.out.println("姓名:" + name) ;
  36. Blob b = rs.getBlob(2) ;
  37. File f = new File("d:" + File.separator + "loadmldn.gif") ; // 图片文件
  38. OutputStream out = null ;
  39. out = new FileOutputStream(f) ;
  40. out.write(b.getBytes(1,(int)b.length())) ;
  41. out.close() ;
  42. }
  43. pstmt.close() ;
  44. conn.close() ; // 数据库关闭
  45. }
  46. };

吴裕雄--天生自然JAVA数据库编程:处理大数据对象的更多相关文章

  1. 吴裕雄--天生自然JAVA数据库编程:使用JDBC连接ORACLE数据库

    DROP TABLE person ; DROP SEQUENCE myseq ; CREATE SEQUENCE myseq ; CREATE TABLE person ( id INT PRIMA ...

  2. 吴裕雄--天生自然JAVA数据库编程:使用元数据分析数据库

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  3. 吴裕雄--天生自然JAVA数据库编程:事务处理

    DROP TABLE user ; -- 删除表 CREATE TABLE user( id INT AUTO_INCREMENT PRIMARY KEY , name VARCHAR(30) NOT ...

  4. 吴裕雄--天生自然JAVA数据库编程:JDBC2.0操作

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  5. 吴裕雄--天生自然JAVA数据库编程:CallableStatement接口

    DELIMITER // DROP PROCEDURE myproc // -- 删除过程 CREATE PROCEDURE myproc(IN p1 int,INOUT p2 int,OUT p3 ...

  6. 吴裕雄--天生自然JAVA数据库编程:PrepareStatement

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  7. 吴裕雄--天生自然JAVA数据库编程:ResultSet接口

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  8. 吴裕雄--天生自然JAVA数据库编程:执行数据库更新操作

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.Statement ; public clas ...

  9. 吴裕雄--天生自然JAVA数据库编程:JDBC操作步骤及数据库连接操作

    public class ConnectionDemo01{ // 定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gj ...

随机推荐

  1. 前缀和-Big Water Problem (牛客)

    链接:https://ac.nowcoder.com/acm/problem/15164 题目描述 给一个数列,会有多次询问,对于每一次询问,会有两种操作: 1:给定两个整数x, y, 然后在原数组的 ...

  2. 关系型数据库中的jsonfield字段的优劣

    本人并非专业,开发经验也不太足,有一次在弄一个user数据表时,需要增加一些字段,又懒得去修改数据,就索性把这些属性封装在一个类中,序列化为json数据,存放在数据库的一个字段中了,后来,发现这么做至 ...

  3. 解决CentOS7用yum安装软件显示错误:cannot find a valid baseurl for repo: base/7/x86_64

    使用yun安装软件时有时会报repo文件的错误,, 主要问题出自于CentOS-Base.repo文件 解决方案:将这个文件后缀名修改使这个文件无效 [root@localhost ~]# cd /e ...

  4. [原]Greenplum failed segment的恢复方法

    当在使用greenplum过程中有不当的操作时,可能会出现segment节点宕掉的情况(比如在greenplum运行的过程中停掉其中几台segment节点的服务器),通过下面的方法可以恢复segmen ...

  5. ES建立索引步骤, 1,index 2.mapping 3,别名

    1.建立索引PUT /index_trans_detail 2.建立mappingPOST /index_trans_detail/type_trans_detail/_mapping{ " ...

  6. 如何为开发项目编写规范的README文件

    前言 了解一个项目,首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme ...

  7. Win Tomcat8 占用内存过高

    1.解压版 找到tomcat/bin/catalina.bat 文件,修改对应参数 2.安装版 windows服务执行的是bin/tomcat.exe.他读取注册表中的值,而不是catalina.ba ...

  8. slice 、 substr 、replace

    slice( 参数1  [,参数2] )        (注意不要让[参数1]下标越过[参数2]下标,否则会得到空字符串,且[参数2]是不包含在截取范围内的) 参数1:截取字符的[起始下标]. 值为正 ...

  9. Java基础知识笔记第二章:基本数据类型与数组

    标识符和关键字 标识符: 1:字母,数字,下划线,美元符号 2.不能以数字开头 3.标识符不能是:true   false   null(尽管true   false   null不是java的关键字 ...

  10. 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制

    原文:http://blog.csdn.net/jason0539/article/details/18075293 这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见 ...