用JDBC实现对大段文本和媒体文件的访问

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40178464/article/details/81612477

1.对大段文本的访问

在数据库中建立一个样表,有索引和数据列就好,对于列的数据类型选择Clob型的。
实现:将文本文件的内容存储到样表中,并读取到另一个文件中
注:关于JdbcUtils类看我的上篇文章:点这里
1.存储:

    public static void saveFile() throws SQLException {
        Connection conn = null;
        // 使用PreparedStatement预处理防止SQL注入
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 建立连接
            conn = JdbcUtils.getConnect();
            // 创建语句
            String sql = "insert into clobb(tes) values (?)";
            ps = conn.prepareStatement(sql);
            // 这里加入文件的地址
            File file = new File("a.txt");
            Reader reader = new BufferedReader(new FileReader(file));
            ps.setCharacterStream(1, reader, file.length());
            // 执行语句,其中i表示操作影响记录的条数
            int i = ps.executeUpdate();
            reader.close();
            System.out.println("i = " + i);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }

2.读取:

    public static void readFile() throws SQLException {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            // 建立连接
            conn = JdbcUtils.getConnect();
            // 创建语句
            String sql = "select tes from clobb";
            st = conn.prepareStatement(sql);
            // 执行语句
            rs = st.executeQuery();
            while (rs.next()) {
                Clob clob = rs.getClob(1);
                Reader reader = clob.getCharacterStream();
                // 可以用这条语句代替上面的两条语句
                // Reader reader = rs.getCharacterStream(1);
                // 写入目标文件
                File file = new File("copy.txt");
                Writer writer = new BufferedWriter(new FileWriter(file));
                char[] buff = new char[1024];
                for (int i; (i = reader.read(buff)) > 0;) {
                    writer.write(buff, 0, i);
                }
                writer.close();
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }    


2.对二进制类型的访问

在数据库中建立一个样表,有索引和数据列就好,对于列的数据类型,如果数据小于150kb选择Blob型的,如果数据大于150kb则选用LongBlob型的。
实现:将文本文件的内容存储到样表中,并读取到另一个文件中
1.存储:

    public static void saveFile() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 建立连接
            conn = JdbcUtils.getConnect();
            // 创建语句
            String sql = "insert into blobb(blo) values (?)";
            ps = conn.prepareStatement(sql);
            // 这里写入文件地址
            File file = new File("D:\\picture.jpg");
            InputStream in = new BufferedInputStream(new FileInputStream(file));
            ps.setBinaryStream(1, in, file.length());
            // 执行语句
            int i = ps.executeUpdate();
            in.close();
            System.out.println("i = " + i);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }

2.读取:

private static void readFile() throws SQLException {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            // 建立连接
            conn = JdbcUtils.getConnect();
            // 创建语句
            String sql = "select blo from blobb";
            st = conn.prepareStatement(sql);
            // 执行语句
            rs = st.executeQuery();
            while (rs.next()) {
                Blob blob = rs.getBlob(1);
                InputStream in = blob.getBinaryStream();
                // 可以用这条语句代替上面的两条语句
                // InputStream in = rs.getBinaryStream(1);
                // 写入目标文件
                File file = new File("F:\\picture.jpg");
                OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                byte[] buff = new byte[1024];
                for (int i; (i = in.read(buff)) > 0;) {
                    out.write(buff, 0, i);
                }
                out.close();
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }


或许有人就问了,我怎么知道要访问的表存储的是文本文档还是二进制文件呢?
关于这个问题你可以在IDE的datebase里点开要访问的表看一看:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_40178464/article/details/81612477