//新的一年祝大家开年好运
//创建数据库:
CREATE TABLE imags (
id int(11) NOT NULL,
name varchar(255) DEFAULT NULL,
imag longblob,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
//进行代码演示:
package com.memory.imags.cckj;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DButil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test";//test指的是数据库名
private static String user = "root";//连接数据库账号
private static String password = "sa123456";//连接数据库密码
private static Connection conn;
private static PreparedStatement ps;
private static ResultSet rs;
/**
* 获得连接 这里单独封装一个获取数据库连接的方法
*/
public static void getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())//判断是否连接到数据库
System.out.println("Succeeded connecting to the Database!");
} catch (SQLException e) {
System.out.println("jdbc:"+e.getMessage() );
} catch (ClassNotFoundException e) {
System.out.println("jdbc:"+e.getMessage());
}
}
/**
* 关闭连接 这里单独封装了一个close方法用来关闭资源
*/
public static void close() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
System.out.println("连接关闭异常:"+e.getMessage());
}
}
public void add() {
//sql插入语句
String sql = "insert into imags values(?,?,?) ";
//将本地图片插入到数据库中
File file=new File("C:\\Users\\mldn\\Desktop\\login1.jpg");
getConnection();//调用方法获取数据库连接
try {
FileInputStream fi = new FileInputStream(file);
ps = conn.prepareStatement(sql);//执行sql语句
ps.setInt(1, 2);
ps.setString(2, "image1");
ps.setBlob(3, fi);
int f = ps.executeUpdate();
System.out.println("打印输出:"+f);
if (f > 0) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
//养成好习惯,关闭资源
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void select() {
//获取数据的连接
getConnection();
Blob get_image; //接收从数据获取到的图片类型的数据
String sql = "select * from imags";
try {
// 将读取到的图片存放到指定的路径中,并命名为bb.jpg
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\mldn\\Desktop\\img\\bb.jpg");
ps = conn.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
get_image = resultSet.getBlob("imag");
//将读取到的Blob对象转成字节流
InputStream inputStream = get_image.getBinaryStream();
int a;
byte b[] = new byte[1024];
while ((a = inputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, a);
}
}
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//测试方法
public static void main(String[] args) {
DButil dbutil = new DButil();
//调用方法向数据中存入图片
dbutil.add();
//调用方法将数据库中的图片读出,并放到本地。
dbutil.select();
}
}
图片成功插入到数据库中:
图片成功有数据读取到本地: