将文件存储到数据库中(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();
}
}
總結
以上是生活随笔為你收集整理的将文件存储到数据库中(MySQL)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中通打印助手-实现快递面单快速打印(免费
- 下一篇: 吸音板的价格是多少