豁出去了,把分全给你们了,给点回答吧?

pingju 2002-04-15 03:08:16
我想把一个图象文件,插入到oracle的blob型字段里,然后再读出来,显示,怎么做?
能不能给个完全好使的历程?
...全文
100 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimjxr 2002-04-16
  • 打赏
  • 举报
回复
看下Oracle JDBC手册,第7章讲了怎么写blob,第20章有完整例子。
pingju 2002-04-16
  • 打赏
  • 举报
回复
关注
pingju 2002-04-16
  • 打赏
  • 举报
回复
kuai guan zhu a
pingju 2002-04-16
  • 打赏
  • 举报
回复
哥们,你这方法不行啊,
lRs.updateBinaryStream("pic",outstream,bAtt.length);不行,中间的参数应该是inputstream型的,而我的程序里没有inputstream啊!
kkhui 2002-04-16
  • 打赏
  • 举报
回复
你用的jdbc是?
pingju 2002-04-16
  • 打赏
  • 举报
回复
原风不动的copy执行,还是不行,
dengmj 2002-04-16
  • 打赏
  • 举报
回复
Write file into DB as Large Object.
> String query = null;
> int length = 0;
> Connection connM = null;
> byte[] byteArray = null;
> try {
> File file = new File(filename);
> FileInputStream fis = new FileInputStream(file);
> int c = fis.available();
> byteArray = new byte[c];
> int get = fis.read(byteArray);
> fis.close();
> length = byteArray.length;
> System.out.println("c= " +c +" get= " +get +" byteArray.length= "
+length);
>
> connM = DriverManager.getConnection(url, account, password);
> connM.setAutoCommit(false);
> InputStream bis = new ByteArrayInputStream(byteArray);
> query = "INSERT INTO tests (imgname, imgoid) VALUES ('" +filename +"',
?);";
> PreparedStatement ps = connM.prepareStatement(query);
> ps.setBinaryStream(1, bis, length);
> int result = ps.executeUpdate();
> connM.commit();
> ps.close();
> bis.close();
> System.out.println("filename= " +filename +" file.length= " +length +"
> result= " +result);
> } catch(SQLException sql) {
> sql.printStackTrace();
> } catch(FileNotFoundException fnf) {
> fnf.printStackTrace();
> } catch(IOException ioe) {
> ioe.printStackTrace();
> } finally {
> try {
> connM.setAutoCommit(true);
> } catch (Exception againse) {
> System.out.print("write Exception again at setAutoCommit(true)");
> System.exit(1);
> }
> }
>
全能的帅巨人 2002-04-15
  • 打赏
  • 举报
回复
在ResultSet中的确没有updateBlob方法。但有updateBinaryStream方法。
我建议你把语句
lRs.updateBlob("pic",blob);
更改为
lRs.updateBinaryStream("pic",outstream,bAtt.length);
试试。
有问题你可以看看JDK的API。
http://java.sun.com/products/jdk/1.2/docs/api/index.html
pingju 2002-04-15
  • 打赏
  • 举报
回复
块关注阿
pingju 2002-04-15
  • 打赏
  • 举报
回复
谢谢,你的方法我试了,在插入时候报错,说没找到lRs.updateBlob("pic",blob);
Found 1 errors in JSP file:
C:\\Program Files\\Allaire\\JRun\\servers\\default\\default-app\\market\\test.jsp:33: Error: No method named "updateBlob" was found in type "java/sql/ResultSet".


我的源文件是
<%@ page language="java" import="java.sql.*,oracle.jdbc.*,oracle.jdbc2.*,oracle.sql.*,java.io.*" %>

<%
java.sql.Connection lConn = null;
java.sql.Statement lStat = null;
java.sql.ResultSet lRs = null;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//DBConnectionManager lDB = DBConnectionManager.getInstance();
//lConn = lDB.getConnection("ORACLE");
lConn = DriverManager.getConnection("java:oracle:thin:@srv:1521:srv","yp","yp");
lStat = lConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//lStat.executeUpdate("insert id,EMPTY_BLOB() from tab where id=2");
lStat.executeUpdate("INSERT INTO my_blob_table VALUES ('2', empty_blob())");

lRs = lStat.executeQuery("select pic from my_blob_table where id='2'");
if(lRs.next())
{
oracle.sql.BLOB blob = (oracle.sql.BLOB)lRs.getBlob("pic");
OutputStream outstream = blob.getBinaryOutputStream();
byte[] bAtt = "data".getBytes();
outstream.write(bAtt,0,bAtt.length);
outstream.close();
lRs.updateBlob("pic",blob);
}

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{lRs.close();}catch(Exception e){}
try{lStat.close();}catch(Exception e){}
try{lConn.close();}catch(Exception e){}
}


//rs.updateBlob("C",blob);
%>
dengmj 2002-04-15
  • 打赏
  • 举报
回复
如何保存图片到数据库

java.sql.Connection lConn = null;
java.sql.Statement lStat = null;
java.sql.ResultSet lRs = null;
try
{
DBConnectionManager lDB = DBConnectionManager.getInstance();
lConn = lDB.getConnection("ORACLE");
lStat = lConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
lStat.executeUpdate("insert id,EMPTY_BLOB() from tab where id=2");
lRs = lStat.executeQuery("select pic from tab where id=2");
if(lRs.next())
{
oracle.sql.BLOB blob = (oracle.sql.BLOB)lRs.getBlob("pic");
OutputStream outstream = blob.getBinaryOutputStream();
byte[] bAtt = "data".getBytes();
outstream.write(bAtt,0,bAtt.length);
outstream.close();
lRs.updateBlob("pic",blob);
}

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{lRs.close();}catch(Exception e){}
try{lStat.close();}catch(Exception e){}
try{lConn.close();}catch(Exception e){}
}


读出数据库再显示
<%@ page language="java" import="java.sql.*,java.util.*"%>
<%
String image_id = (String) request.getParameter("ID");
if (image_id != null){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance","java","java");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM IMMAGINE WHERE IMMAGINE_ID = " + image_id);
if (rs.next())
{
String dim_image = rs.getString("IMMAGINE_DIMENSIONE");
byte [] blocco = rs.getBytes("IMMAGINE_IMMAGINE");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
for(int i=0;i<Integer.parseInt(dim_image);i++)
{
op.write(blocco[i]);
}
}
rs.close();
stmt.close();
con.close();
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}
}
%>








81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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