1.首先建立数据库。。。建立一个名为books的数据库,建立一个fs表。
create database if not exists books;
use books;
create table fs(
id int unsigned auto_increment primary key ,
name varchar(50) not null ,
files longblob
);
假如你不喜欢代码建立,也可以直接用数据库管理工机来建立,注意数据类型。
我用的是navicat这个工具。。。
一定不要把数据类型搞错了
准备工作完成后我们建立一个maven项目。。。(不会的小伙伴可以参考这篇博客)
maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
2.建立好maven项目我们要添加依赖:
<dependencies>
<!-- com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
3.建立Java文件。这里用到JDBC的内容和单元测试这两个部分。
(一定要注意我们是在这个包下面建立的Java文件)
4.编写Java程序。(这是将jpg存入数据库)我用的是本地照片。。。
写Java程序时注意你的数据库名字是否正确。
@Test @DisplayName("将文件存入数据库中")
void savefile() {
InputStream is = null;
String file = "C:\\Users\\admin\\Desktop\\girl.jpg";
File f = new File(file);
String fn = f.getName();
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection("jdbc:mysql:/books", "root", "");
PreparedStatement ps = conn.prepareStatement("insert into fs(name,files) values (?,?)");
ps.setString(1, fn);
ps.setBlob(2, is);
int result = ps.executeUpdate();
if (result > 0) {
System.out.println("成功将" + fn + "存入数据库");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
你可以点击鼠标右键将文件保存下来,看是否正确。
你以为这就完了????
远远不够,我们再写一个读取程序,将这个照片读取出来。。。。
5.从数据库中读取文件。
@Test@DisplayName("从数据库中读取文件")
void readfile() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection("jdbc:mysql:/books", "root", "");
PreparedStatement ps = conn.prepareStatement("select * from fs");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
InputStream inputStream = rs.getBinaryStream(3);
OutputStream out = new FileOutputStream(UUID.randomUUID().toString().concat(".jpg"));
byte[] bytes = new byte[10240];
while (inputStream.read(bytes) > 0) {
out.write(bytes);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
读取完成你会发现:
什么你说你想要照片???
照片没有,代码倒是有。。。
Java:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn.scl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.sql.*;
import java.util.UUID;
/**
* <p>Project: filesJDBC - Dome</p>
* <p>Powered by scl On 2023-08-03 19:30:58</p>
* <p>描述:<p>
*
* @author scl [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class Dome {
@Test@DisplayName("从数据库中读取文件")
void readfile() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection("jdbc:mysql:/books", "root", "");
PreparedStatement ps = conn.prepareStatement("select * from fs");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
InputStream inputStream = rs.getBinaryStream(3);
OutputStream out = new FileOutputStream(UUID.randomUUID().toString().concat(".jpg"));
byte[] bytes = new byte[10240];
while (inputStream.read(bytes) > 0) {
out.write(bytes);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Test @DisplayName("将文件存入数据库中")
void savefile() {
InputStream is = null;
String file = "C:\\Users\\admin\\Desktop\\girl.jpg";
File f = new File(file);
String fn = f.getName();
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection("jdbc:mysql:/books", "root", "");
PreparedStatement ps = conn.prepareStatement("insert into fs(name,files) values (?,?)");
ps.setString(1, fn);
ps.setBlob(2, is);
int result = ps.executeUpdate();
if (result > 0) {
System.out.println("成功将" + fn + "存入数据库");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>filesJDBC</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>