BLOB is a reference to data in a database. This example demonstrates how to retrieves bytes from a BLOB.
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");
if (rs.next()) {
// Get the BLOB from the result set
Blob blob = rs.getBlob("col_blob");
// Get the number bytes in the BLOB
long blobLength = blob.length();
// Get bytes from the BLOB in a byte array
int pos = 1; // position is 1-based
int len = 10;
byte[] bytes = blob.getBytes(pos, len);
// Get bytes from the BLOB using a stream
InputStream is = blob.getBinaryStream();
int b = is.read();
}
} catch (IOException e) {
} catch (SQLException e) {
}
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");
if (rs.next()) {
// Get the BLOB from the result set
Blob blob = rs.getBlob("col_blob");
// Get the number bytes in the BLOB
long blobLength = blob.length();
// Get bytes from the BLOB in a byte array
int pos = 1; // position is 1-based
int len = 10;
byte[] bytes = blob.getBytes(pos, len);
// Get bytes from the BLOB using a stream
InputStream is = blob.getBinaryStream();
int b = is.read();
}
} catch (IOException e) {
} catch (SQLException e) {
}
解决方案 »
- 【统计】大家在设计数据库主键用的什么类型【回者都有分】
- 画面数据传递问题
- JNI调用Dll的问题?
- 从session取得的ArrayList,怎么才能防止session的值被修改
- 初学jsp,有个问题搞不懂
- resin编码问题请教,急
- 新做的论坛,请大家帮忙提点意见!谢谢!
- 求教,在jb9下开发,用javamail是否还要下载插件和配置!
- java取ACCESS数据库表的字段
- 兄弟们给一个JAVA树型结构比较优化的算法吧
- 请问有否知道struts中html标签如何支持img图片
- 在文件的下载后存在本地后,文件出现一连串的数字,如何在下载后去掉数字,例:文件是azxs01.zip,下载到本地变成57329847583579azxs01.zi
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
我是往里插入数据,不是读取数据。可以读出来,但视插不进去。
to bruni(不如你):
还是不行。这两个参数是什么意思啊?
user= 用户为空也能访问mysql? 是不是这种方式下用户没有写入的权限,你将user=root试试。
对我用root登陆后添加的匿名用户,不用密码。
可以自己设定访问权限
it is Urgent!
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// rs will be scrollable, will not show changes made by others,
// and will be updatableThen try Blob mapBLOB = (Blob)rsBlob.getBlob(1);
OutputStream bout = mapBLOB.setBinaryStream(1);
bout.write(outBytes,0,outBytes.length); //byte[]outBytes 在先前从文件中读出
bout.close();rs.updateBlob(1, mapBLOB);
rs.updateRow();
下边是我ORACLE例子:你修改一下,应该没问题:
--------------------./........
.........//连库的东东,
title = new String(request.getParameter("title").getBytes("ISO8859-1"),"GB2312");
news1 = new String(request.getParameter("news").getBytes("ISO8859-1"),"GB2312");
tempclass =new String( request.getParameter("class").getBytes("ISO8859-1"),"GB2312");
con.setAutoCommit(false);
String sql = "INSERT INTO news (id,title,news,class) Values (se_dzzw.nextval,'"+title+"', empty_clob(), '"+tempclass+"')";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.executeQuery();
sql="select id from news where rownum<2 order by id desc";
pstmt = con.prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
id = rs.getInt("id"); }
sql = "select news from news where id="+id+" ";
pstmt = con.prepareStatement(sql) ;
rs = pstmt.executeQuery();
oracle.sql.CLOB clobtt = null;
if(rs.next()){
clobtt = (oracle.sql.CLOB)rs.getClob("news");
}
Writer wr = clobtt.getCharacterOutputStream();
wr.write(news1);
wr.flush();
wr.close();
rs.close();
con.commit();
--------------------
不过按你写的执行程序是会报错:说是resultset必须是从以ResultSet.CONCUR_UPDATABLE为参数的,而且resultset必许是从唯一的表中选出来的,还有必须要包含主键。
所以我是这样创建的:
String sqlBlob = "select * from test where ID='***' for update";
然后
Blob mapBLOB = (Blob)rsBlob.getBlob("pic");
不过按你写的执行程序是会报错:说是resultset必须是从以ResultSet.CONCUR_UPDATABLE为参数的,而且resultset必许是从唯一的表中选出来的,还有必须要包含主键。
所以我是这样创建的:
String sqlBlob = "select * from test where ID='***' for update";
然后
Blob mapBLOB = (Blob)rsBlob.getBlob("pic"); //pic为blob型的
最后
rs.updateBlob("pic", mapBLOB);
rs.updateRow();
这样就搞定了,口喜口喜!!!!!
bruni(不如你) 请到这里来:
http://expert.csdn.net/Expert/topic/1964/1964053.xml?temp=.8519403
曲曲几分不成敬意,:)