zoukankan      html  css  js  c++  java
  • 向mysql数据库中插入大二进制文件和大文本

    @Test
         public void run() throws ClassNotFoundException, SQLException,
                    FileNotFoundException {
     
                try {
                    Connection conn = jdbcUtils. getConnection();
                    PreparedStatement pst = conn
                               .prepareStatement( "insert into myblob values(null,?)");
                    File file = new File( "E:\Java\学习\day17-jdbc\视频\1.课程介绍.avi" );
                    FileInputStream fs = new FileInputStream(file);
                    pst.setBinaryStream(1, fs, ( int) file.length());
                     int row = pst.executeUpdate();
                     if (row != 0) {
                         System. out.println( "插入成功");
                    }
               } catch (Exception e) {
                    e.printStackTrace();
               }
     
         }
     
     

    @Test
    public void save() throws ClassNotFoundException, SQLException,
    FileNotFoundException {
    try {
    Connection conn = jdbcUtils.getConnection();
    PreparedStatement pst = conn
    .prepareStatement("insert into mytext values(null,?)");
    File file = new File("E:\Java\Workspaces\DAO\a.txt");
    FileReader fr = new FileReader(file);
    pst.setCharacterStream(1, fr, (int) file.length());
    int row = pst.executeUpdate();
    if (row != 0) {
    System.out.println("插入成功");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

  • 相关阅读:
    Web API系列之三 基本功能实现
    Web API系列之二WebApi基础框架搭建
    C# (类型、对象、线程栈和托管堆)在运行时的相互关系
    C# 命名空间和程序集
    C# new关键字和对象类型转换(双括号、is操作符、as操作符)
    Vue.js系列之四计算属性和观察者
    Vue.js系列之三模板语法
    C# 对象哈希码
    Class与Style绑定
    Koa学习笔记
  • 原文地址:https://www.cnblogs.com/haofaner/p/5652755.html
Copyright © 2011-2022 走看看