电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

MYSQL存取图片(文件)VIP免费

MYSQL存取图片(文件)_第1页
1/11
MYSQL存取图片(文件)_第2页
2/11
MYSQL存取图片(文件)_第3页
3/11
一、 首先,需要知道,数据库本身也存储在硬盘上,也是一种文件,所以数据库存取文件是可能的,那么在存储文件时,要注意两点 1.MYSQL 图片(文件)的类型是 blo b 2.写入数据库的文件必须是二进制流 首先,我们就在 MYSQL 中建立数据库 SQL 脚本如下 view plaincopy to clipboardprint? /*source [SQL 文件的 URL]*/ drop database if exists testImage; create database testImage; use testImage; create table myTable( id int primary key, image blob ); /*source [SQL 文件的 URL]*/ drop database if exists testImage; create database testImage; use testImage; create table myTable( id int primary key, image blob ); 有了这个脚本,就可以进行编码存储图片了,这里给出存取的关键代码 存: view plaincopy to clipboardprint? Class.forName("com.java.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk"; Connection conn = DriverManager.getConnection(url); PreparedStatement pstmt = "INSERT INTO myTable(image) VALUES (?)"; pstmt = conn.prepareStatement(sql); File file = new File("file 的URL"); InputStream imageStream = new FileInputStream(file); pstmt.setBinaryStream(1, imageStream, (int) file.length()); pstmt.executeUpdate(); pstmt.close(); conn.close(); Class.forName("com.java.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk"; Connection conn = DriverManager.getConnection(url); PreparedStatement pstmt = "INSERT INTO myTable(image) VALUES (?)"; pstmt = conn.prepareStatement(sql); File file = new File("file 的URL"); InputStream imageStream = new FileInputStream(file); pstmt.setBinaryStream(1, imageStream, (int) file.length()); pstmt.executeUpdate(); pstmt.close(); conn.close(); 取: view plaincopy to clipboardprint? PreparedStatement pstmt = conn.prepareStatement("select image from myTable where...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

MYSQL存取图片(文件)

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部