⑴ java 中 blob转字符串
//字符串转blob
String str="哈哈哈哈哈哈";
java.sql.Blob bl=new SerialBlob(str.getBytes());
//blob转字符串
BufferedReader bf=new BufferedReader(new InputStreamReader(bl.getBinaryStream()));
String temp="";
StringBuffer sb=new StringBuffer();
while((temp=bf.readLine())!=null)
{
sb.append(temp);
}
System.out.println(sb.toString());
⑵ 如何将byte类型转换blob类型
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Main {
static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
static String username = "username";
static String password = "welcome";
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "java.gif");
stmt.setString(2, "Java Official Logo");
File image = new File("D:\\a.gif");
FileInputStream fis = new FileInputStream(image);
stmt.setBinaryStream(3, fis, (int) image.length());
stmt.execute();
conn.commit();
fis.close();
conn.close();
}
}
⑶ java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.
和blob无关吧
可能是你计算错误
保存blob前 输入看看是什么值
⑷ java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.
你把一下的代码加入到一个类里就可以了,我已经测试过了,由于字数的限制不能把完整的类发上来。
String PSQL = "insert into TESTBLOB(NUMCONTENTID,BLOBCONTENT) " + "values(?,EMPTY_BLOB())";
String SSQL = "select BLOBCONTENT from TESTBLOB where NUMCONTENTID = ? for update";
String USQL = "update TESTBLOB set BLOBCONTENT = ? where NUMCONTENTID = ?";
public WriteBLOB() throws Exception {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
ByteBuffer bb = ByteBuffer.allocate(10836);
bb.position(0);
bb.putInt(1);
//调用 insertBLOB 方法 的contentid 应该使用序列这里为了简单就直接写死了
//下面的插入方式应该是你想要的方法 在数据库中会看到 0000 0001 0000 0000 0000....
//但是你这里好像没有真正的是用 ByteBuffer
byte [] b1 = bb.array();
insertBLOB(conn, 1, b1);
//下面是使用 ByteBuffer 后的插入 在数据库中会看到 0000 0001
//不知道你想要什么样子的,你自己选择不吧
bb.flip();
byte [] b2 = new byte [bb.remaining()];
bb.get(b2);
insertBLOB(conn, 2, b2);
} catch (Exception ex) {
ex.printStackTrace();
conn.rollback();
} finally {
if (conn != null)
conn.close();
}
}
public void insertBLOB(Connection conn, int contentid, byte[] content)
throws Exception {
PreparedStatement pstmt = null;
PreparedStatement pstmt2 = null;
int id = contentid;
try {
pstmt = conn.prepareStatement(PSQL);
pstmt2 = conn.prepareStatement(USQL);
pstmt.setInt(1, id);
try {
pstmt.executeUpdate();//在数据库中插入空对象
} catch (SQLException ex) {
ex.printStackTrace();
}
pstmt = conn.prepareStatement(SSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();//查询新插入的记录
oracle.sql.BLOB pc = null;
while (rs.next()) {
pc = (oracle.sql.BLOB) rs.getBlob(1);
}
byte[] data = content;
pc.putBytes(1, data);
pstmt2.setBlob(1, pc);
pstmt2.setInt(2, id);
pstmt2.executeUpdate();
} finally {
try {
pstmt.close();
pstmt2.close();
} catch (Exception EE) {
}
}
return;
}
⑸ java blob byte[] 互换(jdbc中)
觉得文件麻烦就在存之前转换为String型 数据库取出后在转换成byte[]
不知道还有没有别的方法
⑹ Java怎样能把Oracle的Blob类型的内容导出到本地
BLOB blob = (BLOB) rs.getBlob(5);后,使用blob的getBytes()获取byte[]数据或者使用getBinaryStream() 获取流对象,这样你就可以输出到JSP页面了
⑺ java中 已获取byte[]对象a,如何将a转成java.sql.Blob类型
Blob的
setBytes(long pos,
byte[] bytes)
方法不就可以吗
⑻ 数据库里存储的图片字段定义类型为blob,java里对应的类型为byte[],怎么将图片显示到jsp页面
我把我以前收集的给你贴出来,希望能对你有所帮助
jsp编程从数据库中取出图片
1、读取图片数据testimageout.jsp文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.io.*" %>
<html>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
ResultSet rs=null;
int id=Integer.parseInt(request.getParameter("id"));
String sql = "select image from picturenews where id='"+id+"'";
rs=stmt.executeQuery(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b);i!=-1)
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
}
%>
<body>
</html>
2、取出所要显示的图片showimage.jsp文件
<%@ page contentType="text/html;charset=bg2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>显示数据库图片测试页</title>
</head>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
String sql=new String();
sql = "select id from picturenews";
ResultSet rs=stmt.executeQuery(sql);
//显示最后一条记录的图片
rs.last();
%>
<table>
<tr><td><img src='testimageout.jsp?id=<%=rs.getInt("id")%>'></td></tr>
</table>
</body>
</html>
⑼ java 如何将ByteArrayInputStream转换成Blob类型 加急!!
private byte[] toByteArray(Blob fromBlob) throws SQLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
return toByteArrayImpl(fromBlob, baos);
}
private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) throws SQLException, IOException{
byte[] buf = new byte[10*1024*1024];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally { if (is != null) {
try { is.close(); }
catch (IOException ex) { }
}
}
return baos.toByteArray();
}
⑽ java oracle数据blob的操作
//下面为一个完整的例子,如果用framework,需要做一定修改
写入
publicbooleansaveWordFile(StringfilePath){
Filefile=new
File(filePath);
Connectionconn=getConnection();
try
{
java.sql.Statementst=conn.createStatement();
conn.setAutoCommit(false);
st.execute("insertintotable_name
values(1,empty_blob())");
ResultSetrs=
st
.executeQuery("selectid,wordfromtable_namewhere
id=1forupdate");
if(rs.next()){
BLOBblob
=(BLOB)rs.getBlob("word");
OutputStreamoutStream=
blob.getBinaryOutputStream();
InputStreamfin=new
FileInputStream(file);
byte[]b=new
byte[blob.getBufferSize()];
intlen=0;
while((len=fin.read(b))!=-1){
outStream.write(b,0,
len);
}
fin.close();
outStream.flush();
outStream.close();
conn.commit();
conn.close();
}
}
catch(SQLExceptione){
//TODOAuto-generatedcatch
block
e.printStackTrace();
}catch
(FileNotFoundExceptione){
//TODOAuto-generatedcatch
block
e.printStackTrace();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returntrue;
}
读取
publicvoidgetWordFile(Stringid){
Connectionconn=
getConnection();
java.sql.Statementst;
try
{
st=conn.createStatement();
ResultSetrs=
st.executeQuery("selectid,wordfromtable_namewhere
id='"+id+"'");
if(rs.next()){
BLOBblob=
(BLOB)rs.getBlob("word");
Filefile=new
File("c://filename.doc");
FileOutputStreamoutput=new
FileOutputStream(file);
InputStreaminput=
blob.getBinaryStream();
byte[]buffer=new
byte[1024];
inti=0;
while((i=
input.read(buffer))!=-1){
output.write(buffer,0,
i);
}
}
}catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
修改
publicvoipdateblob(Stringid){
Connection
conn=getConnection();
Statementstem=null;
ResultSetrs=null;
try{
conn.setAutoCommit(false);
stem=conn.createStatement();
rs=stem.executeQuery("selectword
fromtable_namewhereid='"+id+"'forupdate");
if
(rs.next()){
BLOBblob=(BLOB)rs.getBlob("word");
OutputStreamoutStream=blob.getBinaryOutputStream();
InputStreamfin=newFileInputStream("c://2.doc");
byte[]b=newbyte[blob.getBufferSize()];
int
len=0;
while((len=fin.read(b))!=-1){
outStream.write(b,0,len);
}
fin.close();
outStream.flush();
outStream.close();
conn.commit();
conn.close();
}
}catch(Exceptionex){
try{
conn.rollback();
}catch(SQLExceptione){
//TODO
Auto-generatedcatchblock
e.printStackTrace();
}
System.out.println(ex.getMessage());
}
}