mysql数据库检索_使用Java从MySql数据库保存和检索图像

mysql数据库检索

Here you will get an example for save and retrieve image from MySql database using Java.

在这里,您将获得一个使用Java从MySql数据库保存和检索图像的示例。

In development we generally use folders for managing images. But we can store images directly in database using BLOB (Binary Large Object) data type.

在开发中,我们通常使用文件夹来管理图像。 但是我们可以使用BLOB(二进制大对象)数据类型将图像直接存储在数据库中。

MySql has following blob types:

MySql具有以下Blob类型:

TINYBLOB: 255 bytes

TINYBLOB: 255个字节

BLOB: 64 KB

BLOB: 64 KB

MEDIUMBLOB: 16 MB

MEDIUMBLOB: 16 MB

LONGBLOB: 4 GB

LONGBLOB: 4 GB

mysql数据库检索_使用Java从MySql数据库保存和检索图像

Depending upon requirement we can use any type. It is recommend no to use database for storing images with large size because it will increase the database size.

根据要求,我们可以使用任何类型。 建议不要使用数据库来存储大尺寸图像,因为这会增加数据库的大小。

Below I have given an example of how to store and retrieve images from database. The description for table that I used is given below.

下面我给出了一个如何从数据库存储和检索图像的例子。 我使用的表的说明如下。

mysql数据库检索_使用Java从MySql数据库保存和检索图像

使用Java从MySql数据库保存和检索图像 (Save and Retrieve Image from MySql Database Using Java)

如何在数据库中保存图像 (How to Save Image in Database)

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
package com;
import java.sql.*;
import java.io.*;
public class DatabaseImageExample {
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/demo","root","root");
File file=new File("E:\\image.png");
FileInputStream fis=new FileInputStream(file);
PreparedStatement ps=con.prepareStatement("insert into image_table (name,image) values(?,?)");
ps.setString(1,"image 1");
ps.setBinaryStream(2,fis,(int)file.length());
ps.executeUpdate();
ps.close();
fis.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
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
package com ;
import java . sql . * ;
import java . io . * ;
public class DatabaseImageExample {
public static void main ( String args [ ] ) {
try {
Class . forName ( "com.mysql.jdbc.Driver" ) ;
Connection con = DriverManager . getConnection ( "jdbc:mysql://localhost/demo" , "root" , "root" ) ;
File file = new File ( "E:\\image.png" ) ;
FileInputStream fis = new FileInputStream ( file ) ;
PreparedStatement ps = con . prepareStatement ( "insert into image_table (name,image) values(?,?)" ) ;
ps . setString ( 1 , "image 1" ) ;
ps . setBinaryStream ( 2 , fis , ( int ) file . length ( ) ) ;
ps . executeUpdate ( ) ;
ps . close ( ) ;
fis . close ( ) ;
con . close ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}

Above example will take image from location E:\\image.png and save it into database table. Actually you can’t see the image directly in the table. You have to retrieve it from database and then save it to some location. Below example shows how you can do this.

上面的示例将从位置E:\\ image.png中获取图像并将其保存到数据库表中。 实际上,您无法直接在表格中看到该图像。 您必须从数据库中检索它,然后将其保存到某个位置。 下面的示例显示了如何执行此操作。

如何从数据库检索图像 (How to Retrieve Image from Database)

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
package com;
import java.io.*;
import java.sql.*;
public class DatabaseImageExample {
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/demo","root","root");
File file=new File("E:\\image1.png");
FileOutputStream fos=new FileOutputStream(file);
byte b[];
Blob blob;
PreparedStatement ps=con.prepareStatement("select * from image_table");
ResultSet rs=ps.executeQuery();
while(rs.next()){
blob=rs.getBlob("image");
b=blob.getBytes(1,(int)blob.length());
fos.write(b);
}
ps.close();
fos.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
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
package com ;
import java . io . * ;
import java . sql . * ;
public class DatabaseImageExample {
public static void main ( String args [ ] ) {
try {
Class . forName ( "com.mysql.jdbc.Driver" ) ;
Connection con = DriverManager . getConnection ( "jdbc:mysql://localhost/demo" , "root" , "root" ) ;
File file = new File ( "E:\\image1.png" ) ;
FileOutputStream fos = new FileOutputStream ( file ) ;
byte b [ ] ;
Blob blob ;
PreparedStatement ps = con . prepareStatement ( "select * from image_table" ) ;
ResultSet rs = ps . executeQuery ( ) ;
while ( rs . next ( ) ) {
blob = rs . getBlob ( "image" ) ;
b = blob . getBytes ( 1 , ( int ) blob . length ( ) ) ;
fos . write ( b ) ;
}
ps . close ( ) ;
fos . close ( ) ;
con . close ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}

Above example will fetch image from database and save it at location E:\\image1.png.

上面的示例将从数据库中获取图像并将其保存在位置E:\\ image1.png。

Comment below if you facing difficulty to understand above code.

如果您难以理解上面的代码,请在下面评论。

Happy Coding!! ???? ????

快乐编码! ????

翻译自: https://www.thecrazyprogrammer.com/2016/01/save-and-retrieve-image-from-mysql-database-using-java.html

mysql数据库检索