大对象数据LOB的应用(Day_10)

   

当你有永不放弃的精神,全力以赴的态度,你会惊叹自己也能创造奇迹!


  • LOB数据类型概述

于于无结构的数据往往都是大型的,存储量非常大,而LOB(large object)类型主要用来支持无结构的大型数据.

用户可以用LOB数据类型来存储大型的无结构数据,特别是文本,图形,视频和音频等多媒体数据,

系统还提供了随机访问这些LOB类型数据的有效办法,LOB数据类型主要是用来存储大量数据的数据库字段,最大可以存储4G字节的非结构化数据。

  • LOB数据类型可以分为以下几种:

  1.    BLOB:二进制LOB类型,用户存放无结构的二进制数据,最大4GB.
  2.      CLOB:字符LOB类型,用于存放字符数据,最大可以存储4GB.
  3.      NLOB:字符LOB类型,和CLOB相同,支持国家字符集.多字符集 GBK
  4.      BFILE:二进制文件类型,与数据库外的操作系统文件相关联,该文件存储二进制大对象.

  

  • 使用LOB类型数据的限制:

  1. 系统不支持分布式LOB,用户不能在SELECT子句或WHERE子句中使用远程LOB定位器,也不能在DBMS_LOB包的子程序中使用远程定位器,也不能引用包含LOB属性的远程表中的对象.
  2. LOB列不能用于聚集表.
  3. LOB列不能出现在查询语句的GROUP BY,ORDER BY ,DISTINCT(去重复)之后,也不允许出现在分组函数和连接函数中.
  4. LOB类型不能出现在数组的定义中.
  5. LOB类型不能够出现在建有分区索引的表中.
  6. NCLOB类型不能作为对象类型的属性,当可以作为对象类型的方法的参数类型.
  • MySQL数据库对LOB类型数据的操作:

 1 CREATE DATABASE lob;
 2 
 3 USE lob;
 4 
 5 ALTER DATABASE lob DEFAULT CHARACTER SET=utf8;
 6 
 7 CREATE TABLE TEXTCLOB(
 8 
 9     CID INT NOT NULL PRIMARY KEY,
10 
11     CNAME VARCHAR(20),
12 
13     NOTES LONGTEXT
14 
15 );    
16 
17 SELECT * FROM TEXTCLOB;
  • JDBC连接数据库的主要步骤:

1. Class.forName("com.mysql.jdbc.Driver");//反射 类对象 四种

2.获取连接 Connection conn=DriverManager.getConnection(URL,USER,PASSWORD);

3.编写SQL语句并发送 PrepapredStatement pstm=conn.prepareStatement(sql);

4.获得数据库返回结果 (ResultSet rs) 增删改(int)

5.关闭资源 public static void closeResource(Connection conn,PreparedStatement pstm,ResultSet rs)

  • 代码示例:

  1.   工具类:
 1 package com.papercy.jdbc;
 2 
 3 import java.sql.*;
 4 
 5 public class MySQLConnectionUtil {
 6     public static final String URL="jdbc:mysql://127.0.0.1:3306/lob?useUnicode=true&characterEncoding=utf8";
 7     public static final String USER="root";
 8     public static final String PWD="123456";
 9     public static final String Driver="com.mysql.jdbc.Driver";
10 
11     public static Connection connection()
12     {
13         Connection  connection=null;
14         try {
15             Class.forName(Driver);
16             connection= DriverManager.getConnection(URL,USER,PWD);
17             return connection;
18         } catch (SQLException e) {
19             e.printStackTrace();
20         } catch (ClassNotFoundException e) {
21             e.printStackTrace();
22         }
23         return connection;
24     }
25 
26     public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet)
27     {
28 
29             try {
30                 if(resultSet!=null)
31                 {
32                 resultSet.close();
33                 }
34                 if (preparedStatement!=null)
35                 {
36                     preparedStatement.close();
37                 }
38                 if (connection!=null)
39                 {
40                     connection.close();
41                 }
42             } catch (SQLException e) {
43                 e.printStackTrace();
44             }
45 
46     }
47 }

 注:尽量设置为静态方法,封装。

 

 

    2.    测试类(插入文本类型文件,插入图片,到数据库)

 1 package com.papercy.testlob;
 2 
 3 import com.papercy.jdbc.MySQLConnectionUtil;
 4 
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileNotFoundException;
 8 import java.io.InputStream;
 9 import java.sql.Connection;
10 import java.sql.PreparedStatement;
11 import java.sql.SQLException;
12 
13 public class TestLob {
14     public static void main(String[] args) {
15      
16         Connection connection=MySQLConnectionUtil.connection();
17 
18         String sql="INSERT INTO testLob(number,notes,imgtext) VALUES(?,?,?)";
19 
20         PreparedStatement preparedStatement=null;
21 
22         try {
23 
24             preparedStatement=connection.prepareStatement(sql);
25 
26             preparedStatement.setInt(1,20);
27 
28             preparedStatement.setString(2,"demo.txt");
29 
30             File file=new File("dyyandwht.jpg");
31 
32             InputStream inputStream=new FileInputStream(file);
33 
34             preparedStatement.setAsciiStream(3,inputStream);
35 
36             int update=preparedStatement.executeUpdate();
37 
38             if(update>0)
39             {
40                 System.out.println("数据插入成功");
41             }
42             else
43                 System.out.println("数据插入失败");
44 
45         } catch (SQLException e) {
46 
47             e.printStackTrace();
48 
49         } catch (FileNotFoundException e) {
50 
51             e.printStackTrace();
52 
53         }
54         finally {
55 
56             MySQLConnectionUtil.close(connection,preparedStatement,null);
57 
58         }
59 
60     }
61 }
62     

END


PS:

如果,您希望更容易地发现我的新博客,不妨点击一下关注。

如果你觉得本篇文章对你有所帮助,请给予我更多的鼓励,

因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【肥肥也】!

原文地址:https://www.cnblogs.com/papercy/p/13622539.html