java数据库编程——Insert and Retrieve Images from MySql Table Using Java
来源:互联网 发布:2017年淘宝客市场规模 编辑:程序博客网 时间:2024/06/15 18:55
【0】README
0.1)本文翻译自 http://harmeetsingh13.blogspot.jp/2013/03/insert-and-retrieve-images-from-mysql.html
【1】正文如下:
段1)演示 从数据库表中插入和查询出图片。大多数情况下,图片数据都存储在数据库外部的一些文件夹下,而将其路径存储到数据库。但是在一些场景下,我们需要将图片以二进制数据格式存储到数据库。
Create Table in MySQL : CREATE TABLE `image` ( `id` varchar(45) DEFAULT NULL, `size` int(11) DEFAULT NULL, `image` longblob);
段2) 在mysql中,我们使用blob 类型存储这样类型的数据。它 只支持5kb 的图片数据容量,但这也取决于数据库提供商。
【2】代码示例
2.1)向数据库插入图片
import java.sql.*;import java.io.*;public class InsertImagesMysql{public static void main(String[] args){System.out.println("Insert Image Example!");String driverName = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/";String dbName = "test";String userName = "root";String password = "root";Connection con = null;try{ Class.forName(driverName); con = DriverManager.getConnection(url+dbName,userName,password); Statement st = con.createStatement(); File imgfile = new File("pic.jpg"); FileInputStream fin = new FileInputStream(imgfile); PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)"); pre.setString(1,"test"); pre.setInt(2,3); pre.setBinaryStream(3,(InputStream)fin,(int)imgfile.length()); pre.executeUpdate(); System.out.println("Successfully inserted the file into the database!"); pre.close(); con.close(); }catch (Exception e1){System.out.println(e1.getMessage());}}}
2.2)从数据库查询图片并保存到本地
import java.sql.*;import java.io.*;public class RetriveImagesMysql{public static void main(String[] args){System.out.println("Retrive Image Example!");String driverName = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/";String dbName = "test";String userName = "root";String password = "root";Connection con = null;try{Class.forName(driverName);con = DriverManager.getConnection(url+dbName,userName,password);Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("select image from image");int i = 0;while (rs.next()) {InputStream in = rs.getBinaryStream(1);OutputStream f = new FileOutputStream(new File("test"+i+".jpg"));i++;int c = 0;while ((c = in.read()) > -1) {f.write(c);}f.close();in.close();}}catch(Exception ex){System.out.println(ex.getMessage());}}}
0 0
- java数据库编程——Insert and Retrieve Images from MySql Table Using Java
- Using Java arrays to insert, retrieve, & update PostgreSQL arrays
- Azure Basic - Retrieve data from BlockBlob, Table and Queue
- Java——MySQL数据库编程练习
- JAVA数据库编程—mysql JDBC
- java数据库编程--MySQL数据库
- Insert data into a table using table variables and cross join
- MySQL—— Insert record if not exists in table
- Java与MySQL数据库编程
- java学习---mysql数据库编程
- Java 数据库编程之mysql
- Java MySQL数据库编程详解
- Debugging PL-SQL calls from Java Session Using Eclipse and SQL Developer
- MySQL数据库之INSERT,REPLACE,UPDATE ,DELETE,TRUNCATE TABLE
- Lua——table.insert
- Android: Using monkey from Java(三)
- java数据库编程的前期准备——mysql-connector-java-5.1.22
- Multi-table Insert Using Pipelined Function
- 使用FileZilla Ftp 客户端工具上传vxWorks镜像的注意事项
- Web安全测试(一)-手工安全测试方法&修改建议
- HDU 4355 Party All the Time
- 翻翻git之---实用工具类Lazy(绝对的好东西,走过路过别错过)
- js类封装
- java数据库编程——Insert and Retrieve Images from MySql Table Using Java
- MAC使用php7搭建LNMP环境
- JSP 指令
- Linux修改时区
- MyBatis自动创建代码
- Proximal Gradient Descent for L1 Regularization
- Swift 随机数
- 图论汇总
- df 命令结果显示的 Size 不等于 Avail + Used