将文件存储到数据库中(MySQL)
2013-07-20 19:04
176 查看
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();
}
}
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查看存储过程和执行sql脚本文件到数据库中
- Mysql 数据库文件存储在哪个目录
- MySQL 5.7 修改数据库文件存储位置
- 修改mysql 数据库文件默认存储目录
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序
- MySQL查看数据库文件存放目录
- mysql 调整数据库存储路径
- mysql中名字为mysql的数据库的user表,可以存储用户信息,以及ip地址等,可以用来重设密码,禁止或允许用户远程访问等
- Java利用.property文件存储数据库配置信息,方便修改。
- MySQL导入导出数据库文件
- 使用配置文件来存储数据库链接信息时---工具类的编写
- sqlserver2005中作成数据库邮件配置文件的存储过程
- asp下将数据库中的信息存储至XML文件中
- xml做mysql的配置文件及php对配置文件信息的读取 连接数据库
- MySQL新建一个数据库的时,产生的新文件在哪?
- MySql如何执行sql文件 数据库 详细步骤
- linux下mysql自动备份数据库与自动删除临时文件