如何将图片存入MySQL中的blob去
来源:互联网 发布:免费域名小说1080 编辑:程序博客网 时间:2024/06/14 04:23
建立表:
CREATE TABLE example (name VARCHAR(100),city VARCHAR(100),image BLOB,Phone VARCHAR(100));
使用一张图片qqq.jpg
存图片的代码:
package test;import java.sql.*;import java.io.*;public class SaveImageToDatabase {public static void main(String[] args) throws SQLException {// declare a connection by using Connection interfaceConnection connection = null;/* * Create string of connection url within specified format with machine * name, port number and database name. Here machine name id localhost * and database name is mahendra. */String connectionURL = "jdbc:mysql://localhost:3306/test";/* * declare a resultSet that works as a table resulted by execute a * specified sql query. */ResultSet rs = null;// Declare prepare statement.PreparedStatement psmnt = null;// declare FileInputStream object to store binary stream of given image.FileInputStream fis;try {// Load JDBC driver "com.mysql.jdbc.Driver"Class.forName("com.mysql.jdbc.Driver").newInstance();/* * Create a connection by using getConnection() method that takes * parameters of string type connection url, user name and password * to connect to database. */connection = DriverManager.getConnection(connectionURL, "root","root");// create a file object for image by specifying full path of image// as parameter.File image = new File("D:/qqq.jpg");/* * prepareStatement() is used for create statement object that is * used for sending sql statements to the specified database. */psmnt = connection.prepareStatement("insert into example(name, city, image, Phone) "+ "values(?,?,?,?)");psmnt.setString(1, "michael");psmnt.setString(2, "Delhi");psmnt.setString(4, "123456");fis = new FileInputStream(image);psmnt.setBinaryStream(3, (InputStream) fis, (int) (image.length()));/* * executeUpdate() method execute specified sql query. Here this * query insert data and image from specified address. */int s = psmnt.executeUpdate();if (s > 0) {System.out.println("Uploaded successfully !");} else {System.out.println("unsucessfull to upload image.");}}// catch if found any exception during rum time.catch (Exception ex) {System.out.println("Found some error : " + ex);} finally {// close all the connections.connection.close();psmnt.close();}}}
将图片从数据库中取出来,放到文件中:
package com.liang.java;import java.sql.*;import java.io.*;public class GetImageFromDatabase {public static void main(String[] args) throws SQLException {Connection connection = null;String connectionURL = "jdbc:mysql://localhost:3308/thzdatabase";ResultSet rs = null;PreparedStatement psmnt = null;// declare FileInputStream object to store binary stream of given image.FileOutputStream fos;try {File imageout = new File("D:/1111.png");fos=new FileOutputStream(imageout);Class.forName("com.mysql.jdbc.Driver").newInstance();connection = DriverManager.getConnection(connectionURL, "root","111111");psmnt = connection.prepareStatement("select image from test where name=? ");psmnt.setString(1, "michael");rs=psmnt.executeQuery();rs.next();Blob image_blob=rs.getBlob("image");InputStream is=image_blob.getBinaryStream();byte[] b = null;byte [] a = new byte[is.read(b, 0, b.length)];System.out.println(is.toString()); int ch = 0; try { while((ch=is.read()) != -1){ fos.write(ch); } } catch (IOException e1) { e1.printStackTrace(); } finally{ fos.close(); is.close(); }}// catch if found any exception during rum time.catch (Exception ex) {System.out.println("Found some error : " + ex);} finally {// close all the connections.System.out.println("Found success!");connection.close();psmnt.close();}}}
0 0
- 如何将图片存入MySQL中的blob去
- 将图片存入mysql
- PHP将图片存入MYSQL
- VB.NET如何将几百兆甚至1G以上的文件存入ORACLE的BLOB字段里面去!
- 将mysql中的Blob的图片在jsp中显示
- java 将图片存入mysql数据库中
- 如何将图片存入数据库中
- 如何将图片存入数据库中
- Oracle JDBC存入图片Blob
- 如何将java中Date存入mysql中的datetime中,字符串怎么转换为日期类型
- 如何将java中Date存入mysql中的datetime中,字符串怎么转换为日期类型
- Java将zip存入oralce的blob
- .将图片存入数据库
- 将图片存入数据库
- 用Java 将图片存入Mysql 并读出
- python将图片以二进制存入mysql并取出来
- 用Java 将图片存入Mysql 并读出的方法
- MySQL存入图片+Qt读入读出数据库中的图片
- Volley笔记
- 教你一分钟实现动态模糊效果
- 常用电脑知识
- tomcat启动项目异常: 2 字节的 UTF-8 序列的字节 2 无效。
- Swift中的类
- 如何将图片存入MySQL中的blob去
- TensorFlow框架的简单理解
- Linux 初始化 init 系统(UpStart)
- 对于Java回调的最深刻解析
- datatable edit 中 数据源为 HTML类型时的操作 取得超链接中的文字 输出时整理斜杠
- 无法连接 MKS: Login(username/password)incorrect
- android中和屏幕相关的操作
- java assert的使用
- WinForm中如何使用WPF中的控件