JDBC Blob数据类型是什么?如何将数据存储和读取到其中?
BLOB是二进制大对象,可容纳可变量的数据,最大长度为65535个字符。
它们用于存储大量的二进制数据,比如图像或其他类型的文件。定义为TEXT的字段也可容纳大量的数据。两者之间的区别在于,在BLOB中存储的数据的排序和比较是区分大小写的,在TEXT字段中则是不区分大小写的。对于BLOB或TEXT,不需要指定长度。
阅读更多:MySQL 教程
将Blob存储到数据库中
要将Blob数据类型存储到数据库中,使用JDBC程序,请按照以下步骤进行操作
步骤1:连接数据库
您可以使用 DriverManager 类的 getConnection() 方法连接到数据库。
通过将MySQL URL作为参数传递给getConnection()方法,连接到MySQL数据库,该URL是 jdbc:mysql://localhost/sampleDB (其中sampleDB是数据库名称),并将用户名和密码作为参数。
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
步骤2:创建PreparedStatement对象
使用 Connection 接口的 prepareStatement() 方法创建PreparedStatement对象。将包含占位符的插入查询作为参数传递给此方法。
PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTableVALUES(?, ?)");
步骤3:设置占位符的值
使用 PreparedStatement 接口的setter方法,将值设置给占位符。根据列的数据类型选择方法。例如,如果该列是VARCHAR类型,则使用setString()方法;如果该列是INT类型,则可以使用setInt()方法。
如果该列是Blob类型,则可以使用setBinaryStream()或setBlob()方法设置值。将表示参数索引的整数变量和InputStream类的对象作为参数传递给这些方法。
pstmt.setString(1, "sample image");
//插入Blob类型
InputStream in = new FileInputStream("E:\images\cat.jpg");
pstmt.setBlob(2, in);
步骤4:执行语句
使用PreparedStatement接口的 execute() 方法执行上述创建的PreparedStatement对象。
从数据库中检索Blob
ResultSet接口的getBlob()方法接受表示列索引(或表示列名称的字符串值)的整数,并检索指定列处的值,并以Blob对象的形式返回它。
while(rs.next()) {
rs.getString("Name");
rs.getString("Type");
Blob blob = rs.getBlob("Logo");
}
getBytes() 方法检索当前 Blob 对象的内容,并作为字节数组返回。
使用 getBlob() 方法,您可以将Blob的内容检索到字节数组中,并使用 FileOutputStream 对象的write()方法创建图像。
byte byteArray[] = blob.getBytes(1,(int)blob.length());
FileOutputStream outPutStream = new FileOutputStream("path");
outPutStream.write(byteArray);
示例
下面的示例在MySQL数据库中创建一个具有Blob数据类型的表,并将图像插入其中。然后将其检索回来并存储在本地文件系统中。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class BlobExample {
public static void main(String args[]) throws Exception {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//建立连接
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("连接已建立......");
//创建表
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE SampleTable( Name VARCHAR(255), Image BLOB)");
System.out.println("表已创建");
//插入数据
String query = "INSERT INTO SampleTable(Name,image) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "样例图片");
FileInputStream fin = new FileInputStream("E:\images\cat.jpg");
pstmt.setBlob(2, fin);
pstmt.execute();
//检索数据
ResultSet rs = stmt.executeQuery("select * from SampleTable");
int i = 1;
System.out.println("表中的内容为: ");
while(rs.next()) {
System.out.println(rs.getString("Name"));
Blob blob = rs.getBlob("Image");
byte byteArray[] = blob.getBytes(1,(int)blob.length());
FileOutputStream outPutStream = new
FileOutputStream("E:\images\blob_output"+i+".jpg");
outPutStream.write(byteArray);
System.out.println("E:\images\blob_output"+i+".jpg");
System.out.println();
i++;
}
}
}
输出:
连接已建立......
表已创建
表中的内容为:
样例图片
E:\images\blob_output1.jpg
极客教程