blob 是二进制大对象,可以容纳可变数量的数据,最大长度为 65535 个字符。
它们用于存储大量二进制数据,例如图像或其他类型的数据。文件。定义为 text 的字段也保存大量数据。两者之间的区别在于,存储数据的排序和比较在 blob 中区分大小写,而在 text 字段中不区分大小写。您没有使用 blob 或 text 指定长度。
将 blob 存储到数据库要将 blob 数据类型存储到数据库,请使用 jdbc 程序按照以下步骤操作
第 1 步:连接到数据库您可以使用 drivermanagergetconnection() 方法连接到数据库
通过传递 mysql url(jdbc:mysql://localhost/sampledb)(其中 exampledb 是数据库名称)、用户名和密码作为 getconnection() 方法的参数。
string mysqlurl = "jdbc:mysql://localhost/sampledb";connection con = drivermanager.getconnection(mysqlurl, "root", "password");
第二步:创建预编译语句
使用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");//inserting blob typeinputstream in = new fileinputstream("e:\images\cat.jpg");pstmt.setblob(2, in);
第四步:执行语句
使用preparedstatement接口的execute()方法执行上述创建的preparedstatement对象。
从数据库中检索blobresultset接口的getblob()方法接受一个整数,表示列的索引(或者一个表示列名的字符串值),并检索指定列的值,并以blob对象的形式返回。
while(rs.next()) { rs.getstring("name"); rs.getstring("type"); blob blob = rs.getblob("logo");}
blob 接口的 getbytes() 方法检索当前 blob 对象的内容并以字节数组形式返回。使用getblob()方法,您可以将blob的内容获取到字节数组中,并使用write()方法创建图像fileoutputstream 对象。
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 { //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 a table statement stmt = con.createstatement(); stmt.execute("create table sampletable( name varchar(255), image blob)"); system.out.println("table created"); //inserting values string query = "insert into sampletable(name,image) values (?, ?)"; preparedstatement pstmt = con.preparestatement(query); pstmt.setstring(1, "sample image"); fileinputstream fin = new fileinputstream("e:\images\cat.jpg"); pstmt.setblob(2, fin); pstmt.execute(); //retrieving the data resultset rs = stmt.executequery("select * from sampletable"); int i = 1; system.out.println("contents of the table are: "); 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++; } }}
输出connection established......table createdcontents of the table are:sample imagee:\images\blob_output1.jpg
以上就是什么是 jdbc blob 数据类型?如何存储和读取其中的数据?的详细内容。