55-jdbc大数据处理



mysql大数据处理

这里说的大数据可不是高大上的大数据
只是大的数据而已
比较大的数据比如mp3


我们先试试把大数据写入到数据库中

说实话,有些人就只管复制粘贴
从来不自己写博客
我都是一行一行代码写
还加上注释


    public static void main(String[] args) {

        try {
            //加载驱动类
            Class.forName("com.mysql.jdbc.Driver");
            //地址
            String url = "jdbc:mysql://localhost:3306/abc?useSSL=false";
            //账号和密码
            String username = "root";
            String password = "123";

            //连接数据库
            Connection connection = DriverManager.getConnection(url, username, password);

            //来一条sql语句
            String sql = "insert into tb_music values(?,?,?)";

            //获取statement对象
            PreparedStatement statement = connection.prepareStatement(sql);

            //设置参数替换?号
            statement.setInt(1, 1);
            statement.setString(2, "七里香.mp3");

            //把文件转换为byte[]
            byte[] bytes = IOUtils.toByteArray(new FileInputStream("F://七里香.mp3"));
            //定义blob
            Blob blob = new SerialBlob(bytes);
            //设置blob
            statement.setBlob(3, blob);
            //执行更新
            statement.executeUpdate();

            //最后要关闭statement,connection
            statement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



然后是读取,一样的道理




    public static void main(String[] args) {

        try {
            //加载驱动类
            Class.forName("com.mysql.jdbc.Driver");
            //地址
            String url = "jdbc:mysql://localhost:3306/abc?useSSL=false";
            //账号和密码
            String username = "root";
            String password = "123";

            //连接数据库
            Connection connection = DriverManager.getConnection(url, username, password);

            //来一条sql语句
            String sql = "select * from tb_music";

            //获取PreparedStatement对象
            PreparedStatement statement = connection.prepareStatement(sql);

            //获取结果集对象
            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                InputStream stream = resultSet.getBinaryStream(3);
                FileOutputStream outputStream = new FileOutputStream("D://hehe.mp3");
                IOUtils.copy(stream, outputStream);
            }

            //最后要关闭rs,statement,connection
            resultSet.close();
            statement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值