急。。。。oracle.sql.BLOB与InputStream怎么互相转换???请大侠们帮忙!

morethan 2003-09-12 12:22:30
Session Bean 中有一方法,其中有一参数为oracle.sql.BLOB类型。此参数作用是传递二进制流,在这个方法中的操作主要是往ORALCE数据库里写入BLOB类型的数据!

请问oracle.sql.BLOB与InputStream怎么互相转换?

或者提供更好的解决方法?不胜感激!
...全文
504 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
stars_of_leo 2003-09-20
  • 打赏
  • 举报
回复
shou cang
戚爱斌 2003-09-20
  • 打赏
  • 举报
回复
用BYTE[]
morethan 2003-09-13
  • 打赏
  • 举报
回复
up
llpei 2003-09-12
  • 打赏
  • 举报
回复
try {
InputStream in = blob.getBinaryStream();
}
catch (SQLException ex1) {

}
netcobol 2003-09-12
  • 打赏
  • 举报
回复
图片:
import java.sql.*;
import java.io.*;

public class PhotoBean {
Connection conn=null;
ResultSet rs = null;
PreparedStatement psmt=null;
int filesize=0;
public PhotoBean() {
}

private Connection getConnetion(){
String urlConn="jdbc:odbc:sample";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println(DriverManager.getConnection(urlConn));
return DriverManager.getConnection(urlConn);
}
catch (Exception ex) {
System.out.println("connection failed");
}
return conn;
}

public void setBlob(String filename){
String sql="update test set text=? where id=1";
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(new File(filename)));
filesize=dis.available();
}
catch (Exception ex1) {
ex1.printStackTrace();
}

try{
conn = this.getConnetion();
psmt = conn.prepareStatement(sql);
psmt.setBinaryStream(1,dis,filesize);
psmt.executeUpdate();

psmt.close();
conn.close();

}catch(Exception ex){
ex.printStackTrace();
}
}

public byte[] getBlob(){
String sql="select text from test where id=1";
DataInputStream dis=null;
try{
conn = this.getConnetion();
psmt = conn.prepareStatement(sql);
rs=psmt.executeQuery();
rs.next();
dis=new DataInputStream(rs.getBinaryStream(1));
DataInputStream dis1 = new DataInputStream(new FileInputStream(new File("c:\\a.jpg")));
filesize=dis1.available();
byte[] bt=new byte[this.filesize];
dis.read(bt);

psmt.close();
conn.close();
return bt;

}catch(Exception ex){
ex.printStackTrace();
}

return null;
}

public static void main(String[] args){
new PhotoBean().setBlob("c:\\a.jpg");
}

}
netcobol 2003-09-12
  • 打赏
  • 举报
回复
给你一些代码:
public class TestClob {
public static void main(String[] args) {
try {
Properties pro = new Properties();
pro.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
pro.put(Context.PROVIDER_URL, "t3://198.188.168.113:7001");

Context ctx = new InitialContext(pro);
javax.sql.DataSource driver = (javax.sql.DataSource) ctx.lookup("newStarDB");
Connection conn = driver.getConnection();
System.out.println(conn);
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
query =
"update name set text=? where id = 1";
pstmt = conn.prepareStatement(query);
pstmt.setCharacterStream(1,new BufferedReader(new FileReader(new File("c:/read.txt"))), new FileInputStream("c:/read.txt").available());
System.out.println("Buffer =========" + new BufferedReader(new FileReader(new File("c:/read.txt"))));
BufferedReader bread = new BufferedReader(new BufferedReader(new FileReader(new File("c:/read.txt"))));
while(query!= null)
{
System.out.println(query);
query = bread.readLine();
}

pstmt.executeUpdate();

query =
"select text from name where id = 1";
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
rs.next();
Clob clob = rs.getClob(1);
Reader read = clob.getCharacterStream();
bread = new BufferedReader(read);
query = bread.readLine();
while(query!= null)
{
System.out.println(query);
query = bread.readLine();
}

System.out.println("over");

}
catch (Exception ep) {
ep.printStackTrace();
}

}

}
morethan 2003-09-12
  • 打赏
  • 举报
回复
这个我知道,我想问的是相互转换,也就是说把InputStream转换成oracle.sql.BLOB!

如果想通过Session Bean的方法靠参数传递二进制流,应该用哪个类??
hesi726 2003-09-12
  • 打赏
  • 举报
回复
我都不想回答这个问题了!!
你自己看看 ORACLE 带的例子吧!!
在 ORACLE 的 JDBC 目录下面有的!!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧