java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.

java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.,第1张

你把一下的代码加入到一个类里就可以了,我已经测试过了,由于字数的限制不能把完整的类发上来。

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();

connsetAutoCommit(false);

ByteBuffer bb = ByteBufferallocate(10836);

bbposition(0);

bbputInt(1);

//调用 insertBLOB 方法 的contentid 应该使用序列这里为了简单就直接写死了

//下面的插入方式应该是你想要的方法 在数据库中会看到 0000 0001 0000 0000 0000

//但是你这里好像没有真正的是用 ByteBuffer

byte [] b1 = bbarray();

insertBLOB(conn, 1, b1);

//下面是使用 ByteBuffer 后的插入 在数据库中会看到 0000 0001

//不知道你想要什么样子的,你自己选择不吧

bbflip();

byte [] b2 = new byte [bbremaining()];

bbget(b2);

insertBLOB(conn, 2, b2);

} catch (Exception ex) {

exprintStackTrace();

connrollback();

} finally {

if (conn != null)

connclose();

}

}

public void insertBLOB(Connection conn, int contentid, byte[] content)

throws Exception {

PreparedStatement pstmt = null;

PreparedStatement pstmt2 = null;

int id = contentid;

try {

pstmt = connprepareStatement(PSQL);

pstmt2 = connprepareStatement(USQL);

pstmtsetInt(1, id);

try {

pstmtexecuteUpdate();//在数据库中插入空对象

} catch (SQLException ex) {

exprintStackTrace();

}

pstmt = connprepareStatement(SSQL);

pstmtsetInt(1, id);

ResultSet rs = pstmtexecuteQuery();//查询新插入的记录

oraclesqlBLOB pc = null;

while (rsnext()) {

pc = (oraclesqlBLOB) rsgetBlob(1);

}

byte[] data = content;

pcputBytes(1, data);

pstmt2setBlob(1, pc);

pstmt2setInt(2, id);

pstmt2executeUpdate();

} finally {

try {

pstmtclose();

pstmt2close();

} catch (Exception EE) {

}

}

return;

}

//保存文件到SQL Server数据库中

FileInfo fi=new FileInfo(fileName);

FileStream fs=fiOpenRead();

byte[] bytes=new byte[fsLength];

fsRead(bytes,0,ConvertToInt32(fsLength));

SqlCommand cm=new SqlCommand();

cmConnection=cn;

cmCommandType=CommandTypeText;

if(cnState==0) cnOpen();

cmCommandText="insert into "+tableName+"("+fieldName+") values(@file)";

SqlParameter spFile=new SqlParameter("@file",SqlDbTypeImage);

spFileValue=bytes;

cmParametersAdd(spFile);

cmExecuteNonQuery()

//保存文件到Access数据库中

FileInfo fi=new FileInfo(fileName);

FileStream fs=fiOpenRead();

byte[] bytes=new byte[fsLength];

fsRead(bytes,0,ConvertToInt32(fsLength));

OleDbCommand cm=new OleDbCommand();

cmConnection=cn;

cmCommandType=CommandTypeText;

if(cnState==0) cnOpen();

cmCommandText="insert into "+tableName+"("+fieldName+") values(@file)";

OleDbParameter spFile=new OleDbParameter("@file",OleDbTypeBinary);

spFileValue=bytes;

cmParametersAdd(spFile);

cmExecuteNonQuery()

代码中的fileName是文件的完整名称,tableName是要 *** 作的表名称,fieldName是要保存文件的字段名称

你可能是想把数据显示到文本框中,结果你只是在类型后调用了toString(),而数据没有读出来,

下面可能是你的代码

byte[] a=new byte[3];

thistextBox1Text = aToString();

这样就会在文本框里显示SystemByte[],

你可以这样改

byte[] a=new byte[3];

for (int i = 0; i < aLength; i++)

{

thistextBox1Text += a[i]ToString("X2") + " ";

}

就可以显示a[]中的各个值了,已经转换成了16进制两位显示!

这是我以前出现的情况,希望对你有帮助

你的问题和 blob 没有关系。在ByteBuffer 里存的数字就是你在 plsql 看到的那样。请仔细理解 ByteBuffer。

以下是我针对你的问题作的一个例子,你执行一下。

import javanioByteBuffer;

public class TestByteBuffer {

public static void main(String[] args) {

ByteBuffer bb = ByteBufferallocate(10836);

bbposition(0);

bbputInt(1);

Systemoutprintln("赋值后buff的状态 :"+bb);

byte [] b1 = bbarray();

//以下打印的数组不是真正的数值,只是开辟空间后的一个排列顺序

for (int i = 0; i < b1length; i++) {

Systemoutprint(b1[i]);

}

Systemoutprintln();

bbflip();

Systemoutprintln("反转后buff的状态 :"+bb);

byte [] b2 = new byte [bbremaining()];

//b2中的数字才是buff中真实的数字,千万不要混淆。

bbget(b2);

Systemoutprint("从 buff 中取到的数值 :");

for (int i = 0; i < b2length; i++) {

Systemoutprint(b2[i]);

}

Systemoutprintln();

Systemoutprintln("取值后buff的状态 :"+bb);

}

} 答案补充 按照你的意思,我写了一个类,已经实现了你的要求。可是我现在最多只能补充回答 500 个字,没有办法发给你。

我已经发到以下地址了,你去看一下吧,应该可以的。

>

以上就是关于java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.全部的内容,包括:java中向oracle数据库blob字段中插入byte[]相关问题, 高人来.、怎样把变量byte[]型数组中二进制数据插入到数据库中、网上查询数据库得到的内容System.Byte[]谁知道怎么解决等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9689791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存