如何使用 JDBC 将图像插入数据库?
**PreparedStatement** 接口的 **setBinaryStream()** 方法接受一个表示参数索引的整数和一个 InputStream 对象,并将参数设置为给定的 InputStream 对象。当需要发送非常大的二进制值时,可以使用此方法。
SQL 数据库提供了一种名为 Blob(二进制大型对象)的数据类型,您可以在其中存储诸如图像之类的较大二进制数据。
使用 JDBC 存储图像
如果需要使用 JDBC 程序将图像存储到数据库中,请创建一个具有 Blob 数据类型的表,如下所示:
CREATE TABLE Tutorial(Name VARCHAR(255), Type INT NOT NULL, Logo BLOB);
现在,使用 JDBC 连接到数据库并准备一个 **PreparedStatement** 来将值插入到上面创建的表中。
String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query);
使用 PreparedStatement 接口的 setter 方法为占位符设置值,并使用 setBinaryStream() 方法为 Blob 数据类型设置值。
FileInputStream fin = new FileInputStream("javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
示例
以下示例演示如何使用 JDBC 程序将图像插入 MySQL 数据库。在这里,我们创建一个具有 blob 数据类型的表,将值插入表中(将 **BinaryStream** 对象插入到 blob 类型中),并检索表的内容。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertingImageToDatabase {
public static void main(String args[]) throws Exception {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql:///sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Executing the statement
String createTable = "CREATE TABLE Tutorial( "
+ "Name VARCHAR(255), "
+ "Type VARCHAR(50), "
+ "Logo BLOB)";
stmt.execute(createTable);
//Inserting values
String query = "INSERT INTO Tutorial(Name, Type, Logo) VALUES (?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "JavaFX");
pstmt.setString(2, "Java_library");
FileInputStream fin = new FileInputStream("E:\images\javafx_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, "CoffeeScript");
pstmt.setString(2, "scripting Language");
fin = new FileInputStream("E:\images\coffeescript_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, "Cassandra");
pstmt.setString(2, "NoSQL database");
fin = new FileInputStream("E:\images\cassandra_logo.jpg");
pstmt.setBinaryStream(3, fin);
pstmt.execute();
System.out.println("Data inserted");
ResultSet rs = stmt.executeQuery("Select *from Tutorial");
while(rs.next()) {
System.out.print("Name: "+rs.getString("Name")+", ");
System.out.print("Tutorial Type: "+rs.getString("Type")+", ");
System.out.print("Logo: "+rs.getBlob("Logo"));
System.out.println();
}
}
}输出
Connection established...... Data inserted Name: JavaFX, Tutorial Type: Java_library, Logo: com.mysql.jdbc.Blob@7dc5e7b4 Name: CoffeeScript, Tutorial Type: scripting Language, Logo: com.mysql.jdbc.Blob@1ee0005 Name: Cassandra, Tutorial Type: NoSQL database, Logo: com.mysql.jdbc.Blob@75a1cd57
**注意:** 使用 JDBC 程序只能存储和检索 .gif、.jpeg 或 .png 类型的图像。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP