mediumblob在java中用什么类型(java中blob类型是什么类型)

本文目录
java中blob类型是什么类型
blob是数据库二进制对象的类型,图片,文本之类的.
java没有特定类,非要说的话,就是个超大的字节数组~java基本类型中没有blob类型,应该是自定义的一个类。
java 关于blob类型问题
public void save(String vid,String title,String type,String user,String date,String context,String file) throws Exception
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "insert into news(v_id,title,type,person,inputtime,context,attach_docid) values(’"+vid+"’,’"+title+"’,’"+type+"’,’"+user+"’,’"+date+"’,EMPTY_BLOB(),’"+file+"’)";
try {
DBJdbc dbjdbc = new DBJdbc();
conn = dbjdbc.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
String sql1 = "select context from news where v_id =’"+vid+"’ for update"; // 使用"FOR UPDATE"得到表的写锁
rs = stmt.executeQuery(sql1);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob(1); // 得到BLOB对象
OutputStream outout = blob.getBinaryOutputStream(); // 建立输出流
InputStream in = new ByteArrayInputStream(strss.HTMLEncode(context).getBytes()); //字符串转换为数据流
int size = blob.getBufferSize();
byte; // 建立缓冲区
int len;
while ((len = in.read(buffer)) != -1)
outout.write(buffer, 0, len);
in.close();
outout.close();
}
conn.commit();
stmt.close();
conn.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
希望能帮助到你
blob字段java如何处理
1.使用jdk中的方法进行传输。在ResultSet 中有getBlob()方法,在PreparedStatement中有setBlob()方法,所以大多数人都会尝试setBlob
(),getBlob() 进行读写,或者两个数据库之间BLOB的传输。这种方法实际上是行不通的,据网上的一些资料介绍,说sun官方的文档有些方法
都是错误的。
2.使用ResultSet.getBinaryStream 和PreparedStatement.setBinaryStream对BLOB进行读写或两个数据库间的传输。这种方法我自己尝试过,
发现,如果BLOB中存储的是文本文件的话,就没问题,如果是二进制文件,传输就会有问题。
根据自己的经验,以及查阅了Oracle的官方文档,都是使用如下处理方法:
1.新建记录,插入BLOB数据
1.1首先新建记录的时候,使用oracle的函数插入一个空的BLOB,假设字段A是BLOB类型的:
insert xxxtable(A,B,C) values(empty_blob(),’xxx’,’yyyy’)
1.2后面再查询刚才插入的记录,然后更新BLOB,在查询前,注意设置Connection的一个属性:
conn.setAutoCommit(false);如果缺少这一步,可能导致fetch out of sequence等异常.
1.3 查询刚才插入的记录,后面要加“ for update ”,如下:
select A from xxxtable where xxx=999 for update ,如果缺少for update,可能出现row containing the LOB value is not locked
的异常
1.4 从查询到的 BLOB字段中,获取blob并进行更新,代码如下:
BLOB blob = (BLOB) rs.getBlob("A");
OutputStream os = blob.getBinaryOutputStream();
BufferedOutputStream output = new BufferedOutputStream(os);
后面再使用output.write方法将需要写入的内容写到output中就可以了。例如我们将一个文件写入这个字段中:
BufferedInputStream input = new BufferedInputStream(new File("c://hpWave.log").toURL().openStream());
byte; //用做文件写入的缓冲
int bytesRead;
while(-1 != (bytesRead = input.read(buff, 0, buff.length))) {
output.write(buff, 0, bytesRead);
System.out.println(bytesRead);
}
上面的代码就是从input里2k地读取,然后写入到output中。
1.5上面执行完毕后,记得关闭output,input,以及关闭查询到的ResultSet
1.6最后执行conn.commit();将更新的内容提交,以及执行conn.setAutoCommit(true); 改回Connction的属性
2.修改记录,方法与上面的方法类似,
2.1首先更新BLOB以外的其他字段
2.2 使用1.3中类似的方法获取记录
2.3 修改的过程中,注意以下:a 需要更新的记录中,BLOB有可能为NULL,这样在执行blob.getBinaryOutputStream()获取的值可能为
null,那么就关闭刚才select的记录,再执行一次update xxxtable set A = empty_blob() where xxx, 这样就先写入了一个空的BLOB(不是null),然后再
使用1.3,1.4中的方法执行更新记录.b 注意别忘了先执行setAutoCommit(false),以及"for update",以及后面的conn.commit();等。
3.读取BLOB字段中的数据.
3.1 读取记录不需要setAutoCommit(),以及 select ....for update.
3.2 使用普通的select 方法查询出记录
3.3 从ResultSet中获取BLOB并读取,如下:
BLOB b_to = (BLOB) rs.getBlob("A");
InputStream is = b_from.getBinaryStream();
BufferedInputStream input = new BufferedInputStream(is);
byte;
while(-1 != (bytesRead = input.read(buff, 0, buff.length))) {
//在这里执行写入,如写入到文件的BufferedOutputStream里
System.out.println(bytesRead);
}
通过循环取出blob中的数据,写到buff里,再将buff的内容写入到需要的地方
4.两个数据库间blob字段的传输
类似上面1和3的方法,一边获取BufferedOutputStream,另外一边获取BufferedInputStream,然后读出写入,需要注意的是写入所用的
Connection要执行conn.setAutoCommit(false);以及获取记录时添加“ for update ”以及最后的commit();
总结以上方法,其根本就是先创建空的BLOB,再获取其BufferedOutputStream进行写入,或获取BufferedInputStream进行读取
(1)对数据库clob型执行插入操作
*************************************************
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
conn.setAutoCommit(false);
query = "insert into clobtest_table(id,picstr) values(?,empty_clob())";
java.sql.PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,"001");
pstmt.executeUpdate();
pstmt = null
query = "select picstr from clobtest_table where id = ’001’ for update";
pstmt = con.prepareStatement(query)
rs= pstmt.executeQuery();
oracle.sql.CLOB clobtt = null;
if(rs.next()){
clobtt = (oracle.sql.CLOB)rs.getClob(1);
}
Writer wr = clobtt.getCharacterOutputStream();
wr.write(strtmp);
wr.flush();
wr.close();
rs.close();
con.commit();
(2)通过sql/plus查询是否已经成功插入数据库
*************************************************
PL/SQL的包DBMS_LOB来处理LOB数据。察看刚才的插入是否成功。使用DBMS_LOB包的getlength这个procedure来检测是否已经将str存入到picstr字段中了。如:
SQL》 select dbms_lob.getlength(picstr) from clobtest_table;
(3)对数据库clob型执行读取操作
*************************************************
读取相对插入就很简单了。基本步骤和一半的取数据库数据没有太大的差别。
String description = ""
query = "select picstr from clobtest_table where id = ’001’";
pstmt = con.prepareStatement(query);
ResultSet result = pstmt.executeQuery();
if(result.next()){
oracle.jdbc.driver.OracleResultSet ors =
(oracle.jdbc.driver.OracleResultSet)result;
oracle.sql.CLOB clobtmp = (oracle.sql.CLOB) ors.getClob(1);
if(clobtmp==null || clobtmp.length()==0){
System.out.println("======CLOB对象为空 ");
description = "";
}else{
description=clobtmp.getSubString((long)1,(int)clobtmp.length());
System.out.println("======字符串形式 "+description);
}
}
请进!!如何把绘制的图片直接以二进制流存入数据库(java)
怎样在mysql中存储比较大的图片?
如果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的!我将告诉你怎样通过HTML表单来储存这些文件,怎样访问和使用这些文件。
一、本文概述
本文的主要内容如下:
* 在MySQL中建立一个新的数据库
* 一个怎样储存文件的例子程序
* 一个怎样访问文件的例子程序
二、在MySQL中建立一个新的database
首先,你必须在你的MySQL中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,你必须做下列步骤:
1. 进入MySQL控制器
2. 输入命令"create database binary_data;"
3. 输入命令"use binary_data;"
输入如下命令:
"CREATE TABLE binary_data ( id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,description CHAR(50), bin_data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50));" (不能断行)
如果没有意外,数据库 和 表 应该建立好了。
三、一个怎样储存文件的例子程序
用这个例子你可以通过Html表单将文件传输到数据库中。
store.php3
// store.php3 - by Florian Dittmer
?》
// 如果提交了表单,代码将被执行:
if ($submit) {
// 连接到数据库
// (你可能需要调整主机名,用户名和密码)
MYSQL_CONNECT( "localhost", "root", "password");
MySQL_select_db( "binary_data");
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
$result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype)VALUES (’$form_description’,’$data’,’$form_data_name’,’$form_data_size’,’$form_data_type’)");
$id= MySQL_insert_id();
print "This file has the following Database ID: $id";
MYSQL_CLOSE();
} else {
// 否则显示储存新数据的表单
?》
@MySQL_select_db( "binary_data");
$query = "select bin_data,filetype from binary_data where id=$id";
$result = @MYSQL_QUERY($query);
$data = @MYSQL_RESULT($result,0, "bin_data");
$type = @MYSQL_RESULT($result,0, "filetype");
Header( "Content-type: $type");
echo $data;
};
?》
程序必须知道要访问那个文件, 你必须将ID作为一个参数。
例如: 一个文件在数据库中的ID为2. 你可以这样调用它: getdata.php3?id=2
如果你将图片储存在数据库里, 你可以向调用图片一样调用它。
Example: 一个图片文件在数据库中的ID为3. 你可以这样调用它:
五、怎样储存大于1MB的文件
如果你想储存大于1MB的文件,你必须对你的程序、PHP设置、SQL设置进行许多修改。
下面几条也许可以帮助你储存小于24MB的文件:
1) 修改 store.php3,将 MAX_FILE_SIZE 的值改成 24000000。
2) 修改你的PHP设置,在一般情况下,PHP只允许小于2MB的文件,你必须将max_filesize(在php.ini中)的值改成24000000
3) 去掉MYSQL的数据包大小限制,在一般情况下 MYSQL 小于1 MB的数据包。
4) 你必须用以下参数重启你的MYSQL :/usr/local/bin/safe_MySQLd -O key_buffer=16M -O table_cache=128 -O sort_buffer=4M -O record_buffer=1M -O max_allowed_packet=24M
5) 如果仍然出错:可能是超时错误,如果你通过一个很慢的连接来储存一个很大的文件,PHP缺省的时间限制为30秒。你可以将max_execution_time(在php.ini中)的值改为-1
下面是一个老外写的,可以读
Saving Images in MySQL
Sometimes, it’s more convenient to save images in a database than as files.
MySQL and PHP make it very easy to do this. In this article, I will describe
how to save images in a MySQL database and display them later on.
Setting up the database
The difference between any regular text or integer fields and a field that
needs to save an image is the amount of data that is needed to be held in the
field. MySQL uses special fields to hold large amounts of data. These fields
are known as blobs (blob).
Here is the BLOB definition from the MySQL site :
A BLOB is a binary large object that can hold a variable amount of data. The
four BLOB types TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB differ only in the
maximum length of the values they can hold
For more information about MySQL BLOBs check out
hapter/manual_Reference.html#BLOB
Use the next syntax to create a basic table that will hold the images:
CREATE TABLE Images (
PicNum int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Image BLOB
);
Setting the upload script
An example of a file upload front end can be seen at File Uploading by berber
(29/06/99). What we need now is the PHP script that will get the file and
insert it into MySQL. The next script does just that. In the script, I’m
assuming that the name of the file field is "icture".
《?
If($Picture != "none") {
$PSize = filesize($Picture);
$mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize));
unlink($Picture);
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db)
or die("Unable to select database");
mysql_query("INSERT INTO Images (Image) VALUES ’($mysqlPicture’)")
or die("Can’t Perform Query");
}
else {
echo"You did not upload any picture";
}
?》
This is all that is needed to enter the image into the database. Note that in
some cases you might get an error when you try to insert the image into
MySQL. In such a case you should check the maximum packet size allowed by
your MySQL ver. It might be too small and you will see an error about this in
the MySQL error log.
What we did in the above file is :
1. Check if a file was uploaded with If($Picture != "none").
2. addslashes() to the picture stream to avoide errors in MySQL.
3. Delete the temporary file.
3. Connect to MySQL, choose the database and insert the image.
Displaying the Images
Now that we know how to get the images into the database we need to figure
out how to get them out and display them. This is more complicated than
getting them in but if you follow these steps you will have this up and
running in no time.
Since showing a picture requires a header to be sent, we seem to be in an
impossible situation in which we can only show one picture and than we can’t
show anymore Since once the headers are sent we can’t send any more headers.
This is the tricky part. To outsmart the system we use two files. The first
file is the HTML template that knows where we want to display the image(s).
It’s a regular PHP file, which builds the HTML that contains the 《IMG》 tags,
as we want to display them. The second file is called to provide the actual
file stream from the database directly into the SRC property of the 《IMG》
tag.
This is how a simple script of the first type should look like:
《HTML》
《BODY》
《?
mysql_connect($host,$username,$password)
or die("Unable to connect to SQL server");
@mysql_select_db($db)
or die("Unable to select database");
mysql_query("SELECT * FROM Images")
or die("Can’t Perform Query");
While($row=mysql_fetch_object($result)) {
echo "《IMG SRC=\"SecondType.php3?PicNum=$row-》icNum\"》";
}
?》
《/BODY》
《/HTML》
While the HTML is being displayed, the SecondType.php3 file is called for
each image we want to display. The script is called with the Picture ID
(PicNum) which allows us to fetch the image and display it.
The SecondType.php3 file looks like this :
《?
$result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum")
or die("Can’t perform Query");
$row=mysql_fetch_object($result);
Header( "Content-type: image/gif");
echo $row-》Image;
?》
This is the whole theory behind images and MySQL. The scripts in this example
are the basics. You can now enhance these scripts to include thumbnails, set
the images in various positions, enhance the database table to hold an ALT
field, Check the width and height of the images before you insert them into
the database and keep that data in the table too etc...

更多文章:
php session原理(PHP session干嘛用的举个简单易懂的例子)
2026年5月5日 12:15
ibatis批量update多条语句(ibatis处理for循环保存数据)
2026年2月28日 17:00
rival for priority(contend 与rival区别)
2026年6月7日 10:15
flash特效文字(如何使用Flash制作逐渐显示的文字动画)
2026年9月18日 15:45
datagridview怎么清空(如何将datagridview中的数据清空)
2025年9月22日 05:00
什么叫学习?求网络前辈们推荐基本自学的网络教材(最好带光盘)
2026年5月19日 02:30
java的length获取的长度(java如何知道一个串数字长度)
2026年4月11日 20:30
vlookup匹配多行求和(Excel怎么使用vlookup匹配相加)
2025年10月13日 23:15
respectable的最高级(respectable和respectful的区别是什么)
2026年6月3日 12:30
java配置环境变量javac找不到(java1.7.0_09环境变量设置后能显示java 命令但是找不到javac命令)
2025年7月20日 20:45














