Hướng dẫn how to retrieve image from mysql database in java using eclipse - cách lấy hình ảnh từ cơ sở dữ liệu mysql trong java bằng eclipse

Tại đây bạn sẽ nhận được một ví dụ để lưu và truy xuất hình ảnh từ cơ sở dữ liệu MySQL bằng Java.

Trong phát triển, chúng ta thường sử dụng các thư mục để quản lý hình ảnh. Nhưng chúng ta có thể lưu trữ hình ảnh trực tiếp trong cơ sở dữ liệu bằng cách sử dụng kiểu dữ liệu Blob [đối tượng lớn nhị phân].

MySQL có các loại blob sau:

Tinyblob: 255 byte 255 bytes

BLOB: 64 KB 64 KB

Mediumblob: 16 MB 16 MB

Longblob: 4 GB 4 GB

Tùy thuộc vào yêu cầu, chúng tôi có thể sử dụng bất kỳ loại. Nên sử dụng cơ sở dữ liệu để lưu trữ hình ảnh có kích thước lớn vì nó sẽ tăng kích thước cơ sở dữ liệu.

Dưới đây tôi đã đưa ra một ví dụ về cách lưu trữ và truy xuất hình ảnh từ cơ sở dữ liệu. Mô tả cho bảng mà tôi đã sử dụng được đưa ra dưới đây.

Cách lưu hình ảnh trong cơ sở dữ liệu

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

packagecom;com;

importjava.sql.*;java.sql.*;

importjava.io.*;java.io.*;

publicclassDatabaseImageExample{classDatabaseImageExample{

publicstaticvoidmain[Stringargs[]]{ staticvoidmain[Stringargs[]]{

try{{

Class.forName["com.mysql.jdbc.Driver"];.forName["com.mysql.jdbc.Driver"];

Kết nối con = DriverManager.GetConnection ["JDBC: mysql: // localhost/demo", "root", "root"];con=DriverManager.getConnection["jdbc:mysql://localhost/demo","root","root"];

Tệp tệp = newfile ["E: \\ iMage.png"];file=newFile["E:\\image.png"];

FileInputStream fis = newFileInputStream [tệp];fis=newFileInputStream[file];

PrepPhentStatement PS = con.Preparestatement ["Chèn vào Image_Table [Tên, Hình ảnh] Giá trị [?,?]"];ps=con.prepareStatement["insert into image_table [name,image] values[?,?]"];

Ps.SetString [1, "Hình ảnh 1"];.setString[1,"image 1"];

ps.setBinaryStream[2,fis,[int]file.length[]];.setBinaryStream[2,fis,[int]file.length[]];

ps.executeUpdate[];.executeUpdate[];

ps.close[];.close[];

fis.close[];.close[];

con.close[];.close[];

}catch[Exceptione]{catch[Exceptione]{

e.printStackTrace[];.printStackTrace[];

}

}

}

Ví dụ trên sẽ lấy hình ảnh từ vị trí E: \\ Image.png và lưu nó vào bảng cơ sở dữ liệu. Trên thực tế, bạn có thể nhìn thấy hình ảnh trực tiếp trong bảng. Bạn phải lấy nó từ cơ sở dữ liệu và sau đó lưu nó vào một số vị trí. Ví dụ dưới đây cho thấy cách bạn có thể làm điều này.

Cách truy xuất hình ảnh từ cơ sở dữ liệu

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

packagecom;com;

importjava.io.*;java.io.*;

importjava.sql.*;java.sql.*;

publicclassDatabaseImageExample{classDatabaseImageExample{

publicstaticvoidmain[Stringargs[]]{ staticvoidmain[Stringargs[]]{

try{{

Class.forName["com.mysql.jdbc.Driver"];.forName["com.mysql.jdbc.Driver"];

Kết nối con = DriverManager.GetConnection ["JDBC: mysql: // localhost/demo", "root", "root"];con=DriverManager.getConnection["jdbc:mysql://localhost/demo","root","root"];

Tệp tệp = newfile ["E: \\ iMage.png"];file=newFile["E:\\image1.png"];

FileInputStream fis = newFileInputStream [tệp];fos=newFileOutputStream[file];

byteb[];b[];

PrepPhentStatement PS = con.Preparestatement ["Chèn vào Image_Table [Tên, Hình ảnh] Giá trị [?,?]"];blob;

Ps.SetString [1, "Hình ảnh 1"];ps=con.prepareStatement["select * from image_table"];

}rs=ps.executeQuery[];

while[rs.next[]]{[rs.next[]]{

blob=rs.getBlob["image"];=rs.getBlob["image"];

b=blob.getBytes[1,[int]blob.length[]];=blob.getBytes[1,[int]blob.length[]];

fos.write[b];.write[b];

}

ps.close[];.close[];

fos.close[];.close[];

con.close[];.close[];

}catch[Exceptione]{catch[Exceptione]{

e.printStackTrace[];.printStackTrace[];

}

}

}

Ví dụ trên sẽ lấy hình ảnh từ vị trí E: \\ Image.png và lưu nó vào bảng cơ sở dữ liệu. Trên thực tế, bạn có thể nhìn thấy hình ảnh trực tiếp trong bảng. Bạn phải lấy nó từ cơ sở dữ liệu và sau đó lưu nó vào một số vị trí. Ví dụ dưới đây cho thấy cách bạn có thể làm điều này.

Cách truy xuất hình ảnh từ cơ sở dữ liệu

Tệp tệp = newfile ["E: \\ Image1.png"];

Bài Viết Liên Quan

Chủ Đề