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转字符串?
//字符串转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());
java blob byte[] 互换(jdbc中)
觉得文件麻烦就在存之前转换为String型 数据库取出后在转换成byte[]
不知道还有没有别的方法
Java中向Oracle数据库Blob字段中插入byte[]相关问题, 高手来
你的问题和 blob 没有关系。在ByteBuffer 里存的数字就是你在 plsql 看到的那样。请仔细理解 ByteBuffer。
以下是我针对你的问题作的一个例子,你执行一下。
import java.nio.ByteBuffer;
public class TestByteBuffer {
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.allocate(10836);
bb.position(0);
bb.putInt(1);
System.out.println(“赋值后buff的状态 :”+bb);
byte [] b1 = bb.array();
//以下打印的数组不是真正的数值,只是开辟空间后的一个排列顺序
for (int i = 0; i b1.length; i++) {
System.out.print(b1[i]);
}
System.out.println();
bb.flip();
System.out.println(“反转后buff的状态 :”+bb);
byte [] b2 = new byte [bb.remaining()];
//b2中的数字才是buff中真实的数字,千万不要混淆。
bb.get(b2);
System.out.print(“从 buff 中取到的数值 :”);
for (int i = 0; i b2.length; i++) {
System.out.print(b2[i]);
}
System.out.println();
System.out.println(“取值后buff的状态 :”+bb);
}
} 答案补充 按照你的意思,我写了一个类,已经实现了你的要求。可是我现在最多只能补充回答 500 个字,没有办法发给你。
我已经发到以下地址了,你去看一下吧,应该可以的。