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

pingju 2002-04-15 03:08:16
我想把一个图象文件,插入到oracle的blob型字段里,然后再读出来,显示,怎么做?
能不能给个完全好使的历程?
...全文
47 点赞 收藏 11
写回复
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);
> }
> }
>
回复
在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());
}
}
%>








回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告