zoukankan      html  css  js  c++  java
  • mysql插入文本文档及读取

    1、把本地的一个文件插入到数据库中,数据库字段用text保存

    public static void main(String[] args) {

    PropKit.use(“pro.txt”);
    DruidPlugin druid = new DruidPlugin(PropKit.get(“jdbcUrl”), PropKit.get(“user”), PropKit.get(“password”));
    druid.start();
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
    arp.start();
    Connection conn = null;
    PreparedStatement st = null;
    try {
    conn= DbKit.getConfig().getConnection();
    String sql = “insert into test(name) values(?)”;
    st = conn.prepareStatement(sql);
    String path =”1.text”;
    File file = new File(path);
    st.setCharacterStream(1, new FileReader(file), file.length());
    int num = st.executeUpdate(); //执行向数据库中插入
    if(num > 0) {
    System.out.println(“插入成功”);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    2、从数据库中读取内容,然后以文本的形式保存到本地

    public static void main(String[] args) {
    PropKit.use(“pro.txt”);
    DruidPlugin druid = new DruidPlugin(PropKit.get(“jdbcUrl”), PropKit.get(“user”), PropKit.get(“password”));
    druid.start();
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
    arp.start();
    Connection conn = null;
    PreparedStatement st = null;
    ResultSet rs=null;
    try {
    conn= DbKit.getConfig().getConnection();
    String sql = “select name from test where id=?”;
    st = conn.prepareStatement(sql);
    st.setInt(1, 1);
    rs = st.executeQuery(); //执行sql语句
    if(rs.next()){
    Reader reader = rs.getCharacterStream(“name”); //获取字段未name的项,也就是我们刚刚存到数据库的1.txt文件
    char buffer[] = new char[1024];
    int len = 0;
    FileWriter out = new FileWriter(“D:\1.txt”); //写到D盘下
    while((len = reader.read(buffer)) > 0){
    out.write(buffer, 0, len);
    }
    out.close();
    reader.close();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

  • 相关阅读:
    vue中插槽的理解
    父子组件的通信
    vue3.0怎么禁用eslint校验代码和修改端口号
    三大排序
    让机器人实现自主行走 没你想的那么难
    国内外知名激光雷达公司盘点
    激光雷达寿命短,思岚通过什么技术来解决?
    浅谈SLAM的回环检测技术
    除了ROS, 机器人定位导航还有其他方案吗?
    思岚科技即将登陆“2018日本机器人周”精彩抢先看
  • 原文地址:https://www.cnblogs.com/wzk1992/p/5735569.html
Copyright © 2011-2022 走看看