zoukankan      html  css  js  c++  java
  • 大对象

    存取大对象---LOB
        【分类】:
            1)Character LOB --> CLOB(Text有四个子类型)
            2)Binary    LOB --> BLOB(Blob有四个子类型)
        
        
        一】CLOB
        
            1.重点类库:
                1)PreaParedStatement类:    
                    重点方法:
                        写:
                            void setCharacterStream(int parameterIndex, Reader reader) ;//将指定参数设置为给定 Reader 对象。
                          void setCharacterStream(int parameterIndex, Reader reader, int length); //将给定参数设置为给定 Reader 对象,该对象具有给定字符数长度。
                          
                        读:
                          Reader getCharacterStream(int columnIndex);//以java.io.Reader 对象的形式获取此 ResultSet 对象的当前行中指定列的值。
                             Reader getCharacterStream(String columnLabel);//以 java.io.Reader 对象的形式获取此 ResultSet 对象的当前行中指定列的值。
                 2)类加载器--ClassLoder类:
                          URL getResource(String name);//查找具有给定名称的资源。
                 3)URL类:
                          String getPath();//获取此 URL 的路径部分。
            2.详细步骤
                    注意:
                        1》在能完成业务的情况下,尽早关闭连接对象
                        2》关闭连接对象,只是不能够发送SQL语句到数据库方,并不意味不能读写。
                                
                    1)写:
                            使用PreparedStatement的setCharacterStream方法+类加载器的getResource()方法+Url类的getPath()方法。
                    code:

     
                    private static void write() throws FileNotFoundException {
                
                        Connection conn = null;
                        PreparedStatement pstmt = null;
                        ResultSet rs = null;
                        String sql = "insert into test_clob(id,context) values(?,?)";
                
                        URL url = Demo1.class.getClassLoader().getResource(
                                "cn/itcast/web/jdbc/config/62.txt");
                        String path = url.getPath();
                        File file = new File(path);
                        System.out.println(file.toString());
                        Long length = file.length();
                        Reader reader = new FileReader(file);
                
                        conn = JdbcUtil.getMySQLConnection();
                        try {
                            pstmt = conn.prepareStatement(sql);
                            pstmt.setString(1, UUID.randomUUID().toString());
                            pstmt.setCharacterStream(2, reader, length);
                
                            int value = pstmt.executeUpdate();
                            if (value > 0) {
                                System.out.println("成功");
                            } else {
                                System.out.println("失败");
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } finally {
                            if(null!=reader){
                                try {
                                    reader.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            JdbcUtil.closeAll(rs, pstmt, conn);
                        }
                
                    }
                }


                
                2)读:
                            使用ResultSet类的getCharacterStream方法
                    code:

               public class Reader {
                            private static Connection conn;
                            private static ResultSet rs;
                            private static java.io.Reader reader;
                            private static java.io.Writer writer;
                            private static PreparedStatement stmt;
                        
                            public static void main(String[] args) {
                                conn = JDBCUtils.getConnection();
                                try {
                                    stmt = conn.prepareStatement(SqlMapping.READ_DATA);
                                    rs = stmt.executeQuery();
                                    rs.next();
                                    reader = rs.getCharacterStream("content");
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                } finally {
                                    JDBCUtils.closeAll(rs, stmt, conn);//需要先关闭SQL的连接,而这并不影响流的连接
                                }
                        
                                try {
                                    int len = 0;
                                    writer = new FileWriter("d:/test.txt");
                                    char[] cbuf = new char[1024];
                                    while ((len = reader.read(cbuf)) > 0) {
                                        writer.write(cbuf, 0, len);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } finally {
                                    if (null != writer) {
                                        try {
                                            writer.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                        
                                    if (null != reader) {
                                        try {
                                            reader.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                        
                            }
                        }
        


        二】CLOB
        
            1.重点类库:
                1)PreaParedStatement类:    
                    重点方法:
                        写:
                            void setBinaryStream(int parameterIndex, InputStream is)  ;//将指定参数设置为给定 is 对象。
                          void setBinaryStream(int parameterIndex, InputStream is, int length); //将给定参数设置为给定 is 对象,该对象具有给定字节长度。
                          
                        读:
                          Reader getBinaryStream(int columnIndex);//以java.io.InputStream 对象的形式获取此 ResultSet 对象的当前行中指定列的值。
                             Reader getBinaryStream(String columnLabel);//以 java.io.InputStream 对象的形式获取此 ResultSet 对象的当前行中指定列的值。
                 2)类加载器--ClassLoder类:
                          URL getResource(String name);//查找具有给定名称的资源。
                 3)URL类:
                          String getPath();//获取此 URL 的路径部分。
            2.详细步骤
                    注意:
                        1》在能完成业务的情况下,尽早关闭连接对象
                        2》关闭连接对象,只是不能够发送SQL语句到数据库方,并不意味不能读写。
                                
                    1)写:
                            使用PreparedStatement的setCharacterStream方法+类加载器的getResource()方法+Url类的getPath()方法。
                    code:

             //将BLOB类型数据存入数据库
                        public class WriteBlob {
                            private static InputStream is;
                            private static Connection conn;
                            private static PreparedStatement stmt;
                            private static ResultSet rs;
                            private static URL fileUrl;
                        
                            public static void main(String[] args) {
                                conn = JDBCUtils.getConnection();
                                try {
                                    fileUrl = WriteBlob.class.getClassLoader().getResource(
                                            "com/suse/source/d1.jpg");
                                    File file = new File(fileUrl.getPath());
                                    is = new FileInputStream(file);
                                    stmt = conn.prepareStatement(SqlMapping.INSERT_PIC);
                                    stmt.setBinaryStream(1, is, file.length());
                                    int rows = stmt.executeUpdate();
                                    if (0 == rows) {
                                        System.out.println("失败!");
                                    } else {
                                        System.out.println("成功!");
                                    }
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                
                                JDBCUtils.closeAll(rs, stmt, conn);
                            }
                        }



                    
                
                2)读:
                            使用ResultSet类的getBinaryStream方法
                    code:

             public class ReadBlob {
                        private static Connection conn;
                        private static ResultSet rs;
                        private static PreparedStatement stmt;
                        private static OutputStream os;
                        private static InputStream is;
                    
                        public static void main(String[] args) {
                            conn = JDBCUtils.getConnection();
                            try {
                                stmt = conn.prepareStatement(SqlMapping.GET_PIC);
                                rs = stmt.executeQuery();
                                if (rs.next()) {
                                    is = rs.getBinaryStream("content");
                                }
                            } catch (SQLException e) {
                                e.printStackTrace();
                            } finally {
                                JDBCUtils.closeAll(rs, stmt, conn);
                            }
                    
                            try {
                                int len = 0;
                                byte[] buf = new byte[1024];
                                os = new FileOutputStream("D:/test.jpg");
                                while ((len = is.read(buf)) > 0) {
                                    os.write(buf, 0, len);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                if (null != is) {
                                    try {
                                        is.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (null != os) {
                                    try {
                                        os.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }



  • 相关阅读:
    毕业论文(设计)开题报告
    jqgrid工作记录1(单元格内容过长三个点显示,表头自动换行,取消滚动条预留空间)
    mysql递归查询(父级,子集)
    ORACLE--SQL日常问题和技巧2(自定义排序,递归查询,异常ORA-01747,逗号隔开的字符串转成in条件,用符号连接表中某字段)
    随机图片获取api
    ubuntu16.04下安装Qt5.9
    Ubuntu16.04更换下载源(图形界面更改)
    VM虚拟机黑屏,但是在运行状态的解决方法之一
    Ubuntu终端快捷复制粘贴
    VIM的部分配置()
  • 原文地址:https://www.cnblogs.com/SkyGood/p/4008084.html
Copyright © 2011-2022 走看看