如何使用 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 数据类型的表,将值插入到表中(Blob 类型的 BinaryStream 对象),并检索表的内容。
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://localhost/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:imagesjavafx_logo.jpg”);
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, “CoffeeScript”);
pstmt.setString(2, “scripting Language”);
fin = new FileInputStream(“E:imagescoffeescript_logo.jpg”);
pstmt.setBinaryStream(3, fin);
pstmt.execute();
pstmt.setString(1, “Cassandra”);
pstmt.setString(2, “NoSQL database”);
fin = new FileInputStream(“E:imagescassandra_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 类型的图像。
以上就是如何使用 JDBC 将图像插入数据库?的详细内容,更多请关注恰卡编程网(mip.qiaqa.com)其它相关文章!
推荐阅读
-
我们如何从现有 MySQL 表的列中删除 FOREIGN KEY 约束?
我们可以通过使用DROP关键字和ALTERTABLE语句从现有表的列中删除FOREIGNKEY约束。语法ALTER...
-
如何改变MySQL表的列位置而不丢失列数据?
借助ALTERTABLE命令,您可以更改MySQL表的列位置而不会丢失数据。语法如下–ALTERTABLEyour...
-
在 MongoDB 中存储日期/时间的最佳方式?
可以通过两种不同的方式在MongoDB中存储日期/时间。在第一种方法中,您可以像JavaScript一样使用Date对...
-
修复 MySQL 数据库错误 #1064?
mysql˃createtableDemoTable(UserIdintNOTNULLAUTO_I...
-
如何在任何 MySQL 表中实现 CANDIDATE 键?
每个关系可能有一个或多个候选键。这些候选键之一称为主键。每个候选键都有资格成为主键。因此,候选主键称为候选键。要在MySQL中...
-
我们如何创建一个在某个指定时间间隔后执行的 MySQL 一次性事件?
示例mysql˃CREATEEVENTtesting_event5ONSCHEDULEATCURRENT_TIMES...
-
如何统计MySQL数据库中表的数量?
要计算表的总数,请使用table_schema的count(*)概念。首先,要检查我们的数据库“business”中有多少...
-
MySQL 如何使用 YEAR 数据类型在表中存储年份值?
MySQL允许声明列YEAR类型,借助它我们可以在该列中存储年份值。mysql˃Createtableyear1(Y...
-
MySQL 和 SQL Server 的区别
MySQL和SQLServer都是关系数据库管理系统或RDBMS。MySQL是开源的,可以免费使用,而SQLSe...
-
如何向现有 MySQL 表添加列?
通过使用ALTER命令,我们可以向现有表添加列。Altertabletable-nameADD(column-name...