zoukankan      html  css  js  c++  java
  • 将文件存储到数据库中(MySQL)

    package com.play;

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

    public class FileToDatabase {

     public static void main(String[] args) throws Exception {
      File file = new File("F:\pig.jpg");
      InputStream inputStream = new FileInputStream(file);
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
      String sql = "insert into t_user(name,image) values(?,?)";
      PreparedStatement ps = con.prepareStatement(sql);
      ps.setString(1, "wangba");
      ps.setBinaryStream(2, inputStream, (int)file.length());
      int rows = ps.executeUpdate();
      System.out.println(rows+"行受影响");
      ps.close();
      con.close();
     }

    }

  • 相关阅读:
    程序员修炼之道:从小工到专家
    2020.12.16收获
    2020.12.15收获
    2020.12.14收获
    2020.12.13收获
    Android学习第二天——对Android的简单了解
    Java学习12.18
    考试加分项
    Java学习12.17
    Java建议
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3202897.html
Copyright © 2011-2022 走看看