1. 테이블을 생성합니다:
다음과 같이 코드 코드를 복사합니다 .
사진이 있으면 테이블을 삭제하세요.
테이블 만들기 사진(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
이름 VARCHAR(100) COMMENT '이름',
사진 덩어리 COMMENT '사진'
)
엔진=InnoDB
기본 문자셋=utf8
COLLATE=utf8_general_ci;
MySql의 그림 데이터 저장 형식은 Blob 유형입니다. Blob은 바이너리 파일을 저장할 수 있는 컨테이너입니다.
2. 이미지 스트림 데이터 액세스를 위한 도구 클래스를 작성합니다.
다음과 같이 코드 코드를 복사합니다 .
java.io.파일 가져오기;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
공개 클래스 ImageUtil {
개인 정적 파일 파일 = null;
/**
* 로컬 파일에서 이미지의 바이너리 스트림을 읽습니다.
*
* @param 파일
* @반품
*/
공개 정적 FileInputStream getImageByte(String infile) {
FileInputStream imageByte = null;
파일 = 새 파일(infile);
노력하다 {
imageByte = new FileInputStream(파일);
} 잡기(FileNotFoundException e) {
e.printStackTrace();
}
이미지바이트를 반환합니다.
}
/**
* 그림 스트림을 그림으로 읽습니다.
*
* @param inputStream
* @param 경로
*/
공개 정적 무효 readBlob(InputStream inputStream, 문자열 경로) {
노력하다 {
FileOutputStream fileOutputStream = new FileOutputStream(경로);
바이트[] 버퍼 = 새 바이트[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(버퍼, 0, len);
}
inputStream.close();
fileOutputStream.close();
} 잡기(FileNotFoundException e) {
e.printStackTrace();
} 잡기(IOException e) {
e.printStackTrace();
}
}
}
3. 로컬 파일을 데이터베이스에 저장
MySql 데이터베이스 드라이버--mysql-connector-java-5.1.24-bin.jar를 추가해야 합니다.
다음과 같이 코드 코드를 복사합니다 .
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
java.sql.DriverManager 가져오기;
import java.sql.PreparedStatement;
import java.sql.SQLException;
공개 클래스 ImageInsert {
공개 정적 무효 메인(String[] args) {
노력하다 {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch(InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} 잡기(ClassNotFoundException e) {
e.printStackTrace();
}
문자열 사용자 = "루트";
문자열 비밀번호 = "루트";
문자열 url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
연결 연결 = null;
노력하다 {
연결 = DriverManager.getConnection(url, 사용자, 비밀번호);
} 잡기(SQLException e) {
e.printStackTrace();
}
ReadyStatement prepareStatement = null;
입력스트림 입력스트림 = null;
inputStream = ImageUtil.getImageByte("D://temp//photo1.png");
노력하다 {
String sql = "사진(id,name,photo) 값(?,?,?)에 삽입";
prepareStatement = 연결.prepareStatement(sql);
prepareStatement.setInt(1, 1);
prepareStatement.setString(2, "줄리");
prepareStatement.setBinaryStream(3, inputStream,
inputStream.available());
prepareStatement.execute();
} 잡기(SQLException e) {
e.printStackTrace();
} 잡기(IOException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if (inputStream != null)
inputStream.close();
} 잡기(IOException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if (preparedStatement != null)
prepareStatement.close();
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
연결.닫기();
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
4. 데이터베이스에서 사진을 읽고 생성합니다.
다음과 같이 코드 코드를 복사합니다 .
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
java.sql.DriverManager 가져오기;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
공개 클래스 ImageGet {
공개 정적 무효 메인(String[] args) {
노력하다 {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch(InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} 잡기(ClassNotFoundException e) {
e.printStackTrace();
}
문자열 사용자 = "루트";
문자열 비밀번호 = "루트";
문자열 url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
연결 연결 = null;
노력하다 {
연결 = DriverManager.getConnection(url, 사용자, 비밀번호);
} 잡기(SQLException e) {
e.printStackTrace();
}
성명서 = null;
결과 집합 결과 집합 = null;
입력스트림 입력스트림 = null;
노력하다 {
문 = 연결.createStatement();
String sql = "id = 1인 사진 p에서 p.photo를 선택합니다.";
resultSet = 문.executeQuery(sql);
결과Set.next();
inputStream = resultSet.getBinaryStream("photo");
ImageUtil.readBlob(inputStream, "D://temp//photo2.png");
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if (inputStream != null)
inputStream.close();
} 잡기(IOException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if (결과세트 != null)
resultSet.close();
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
if(문 != null)
if(문 != null)
노력하다 {
성명.닫기();
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
if (연결 != null)
노력하다 {
연결.닫기();
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
5.오버!