Java web(JDBC)大数据处理[BLOB数据,CLOB数据]

大数据处理

处理CLOB数据

在实际开发中,CLOB用于储存文本的大数据,但是,对于MySQL来说,大数据文本的储存方式是用TEXT类型表示,案例如下:

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CLOB {
     
    public static void main(String[] args) {
     
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
     
            connection = DBUtil.getConnection();
            String sql = "insert into testclob values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            File file = new File("D:/itcast.txt");
            Reader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
            preparedStatement.setInt(1,3);
            preparedStatement.setCharacterStream(2, reader, (int)file.length());//其中三个参数分别为:位置,输入流,文件长度
            preparedStatement.executeUpdate();
        } catch (SQLException | FileNotFoundException | UnsupportedEncodingException e) {
     
            e.printStackTrace();
        }finally {
     
            DBUtil.close(preparedStatement,connection);
        }
    }
}

简单来说,就是将txt中文本,写入MySQL数据库的对应地点。

import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class olbc {
     
    public static void main(String[] args) {
     
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
     
            connection = DBUtil.getConnection();
            String sql = "select * from testclob";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
     
                Reader reader = resultSet.getCharacterStream("resume");
                Writer out = new FileWriter("resume.txt");
                int temp;
                while ((temp =reader.read())!=-1){
     
                    out.write(temp);
                }
                out.close();
                reader.close();
            }
    }catch (SQLException | IOException e){
     
        e.printStackTrace();
        }finally {
     
            DBUtil.close(resultSet,preparedStatement,connection);
        }
    }
}

简单来说就是将MySQL数据库的数据读出来,写入txt文本。

处理BLOB数据

简单来说就是将图片转化为二进制代码储存到数据库

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class OLBCE {
     
    public static void main(String[] args) {
     
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
     
            connection = DBUtil.getConnection();
            String sql = "insert into testblob values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            File file = new File("D:/it.png");
            InputStream inputStream = new FileInputStream(file);
            preparedStatement.setBinaryStream(2,inputStream,(int)file.length());
            preparedStatement.executeUpdate();

        } catch (SQLException | FileNotFoundException e) {
     
            e.printStackTrace();
        }finally {
     
            DBUtil.close(preparedStatement,connection);
        }
    }
}

将储存在数据库的二进制代码转化为jpg形式的图片展示出来。

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BLOBE2 {
     
    public static void main(String[] args) {
     
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
     
            connection = DBUtil.getConnection();
            String sql = "select * from testblob where id = 1";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
     
                InputStream inputStream = new BufferedInputStream(resultSet.getBinaryStream("img"));
                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("img.jpg"));
                int temp;
                while ((temp = inputStream.read()) != -1) {
     
                    outputStream.write(temp);
                }
                outputStream.close();
                inputStream.close();
            }
            } catch (SQLException ex) {
     
            ex.printStackTrace();
        } catch (IOException ex) {
     
            ex.printStackTrace();
        }finally {
     
            DBUtil.close(preparedStatement,connection);
        }
    }
}

公用的DButil代码(链接数据库)

import java.sql.*;

public class DBUtil {
     
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/chapter01?serverTimezone=GMT%2B8";
    private static String user = "root";
    private static String pwd = "2001";

    //加载驱动
    static{
     
        try {
     
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
     
            e.printStackTrace();
        }
    }

    //连接对象
    public static Connection getConnection(){
     
        Connection conn = null;
        try {
     
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
        return conn;
    }

    //关闭流
    public static void close(ResultSet rs, Statement st, Connection conn){
     
        try {
     
            if(rs != null){
     
                rs.close();
            }
            if(st != null){
     
                st.close();
            }
            if(conn != null){
     
                conn.close();
            }
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
    }

    public static void close(PreparedStatement pst, Connection conn){
     
        close(null, pst, conn);
    }

}

你可能感兴趣的:(MySQL,数据库,mysql,jdbc,sql,java)