java+oracle问题 以二进制访问。可用byte数组来接收数据。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java.sql.ResultSet getBlob() getBytes() getClob() itjourney(IT之旅) 可以详细点吗,有例子最好,无限感激!! import java.io.*;import java.sql.*;public class BlobOperation{ public static void addLob(long id, String binFile) throws SQLException { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = ConnectionFactory.getConnection(); //换成你自己取连接的方法 con.setAutoCommit(false); String sql = "INSERT INTO Blob_Tbl(id, binfile, bincontent)"; sql += " VALUES(?, ?, ?)"; ps = con.prepareStatement(sql); ps.setLong(1, id); ps.setString(2, binFile); ps.setBlob(3, oracle.sql.BLOB.empty_lob()); ps.executeUpdate(); //DatabaseUtils.closeObject(ps); ps = con.prepareStatement("SELECT bincontent FROM Blob_Tbl WHERE id = " + id + " for update "); rs = ps.executeQuery(); if (rs.next()) { oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob(1); /* write blob content */ OutputStream binOut = binContent.getBinaryOutputStream(); BufferedOutputStream out = new BufferedOutputStream(binOut); BufferedInputStream in = new BufferedInputStream(new FileInputStream(binFile)); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } con.commit(); } catch (Exception e) { e.printStackTrace(); try { con.rollback(); } catch (SQLException se) { } throw new SQLException(e.getMessage()); } finally { DatabaseUtils.closeObject(rs, ps, con); } } public static void fetchLob(long id, String filename) throws SQLException { Connection con = null; Statement st = null; ResultSet rs = null; try { con = ConnectionFactory.getConnection(); String sql = "SELECT * From Blob_Tbl Where id = " + id; st = con.createStatement(); rs = st.executeQuery(sql); while (rs.next()) { String binFile = rs.getString("binfile"); oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob("bincontent"); /* read blob content */ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename)); BufferedInputStream in = new BufferedInputStream(binContent.getBinaryStream()); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } } catch (Exception e) { throw new SQLException(e.getMessage()); } finally { DatabaseUtils.closeObject(rs, st, con); } } public static void main(String[] args) throws Exception { if (args.length == 0) { addLob(1, "a.jpg"); } else { fetchLob(1, args[0]); } }}自己把有的东西改下吧 请教:使用maven后,如何使用svn插件提交代码 小弟在线急等问题答案。。。 一道看似简单的java面试题 这样值多少? 十万火急! 怎么配置TOMCAT5.0.27自带数据库连接池程序 我的weblogic重装之后,用上三四次后再启动就会出现下面的错误,就大家帮忙看一下。 关于邮件服务器设置的问题? 各位帮我解决关于JBuilder7中连接ORACLE的问题?? 求weblogic7.0注册文件,望好心人帮忙! jsp这三个标签分别都是什么区别呢? 输出格式化问题
getBlob()
getBytes()
getClob()
import java.sql.*;
public class BlobOperation
{
public static void addLob(long id, String binFile) throws SQLException
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null; try
{
con = ConnectionFactory.getConnection(); //换成你自己取连接的方法
con.setAutoCommit(false); String sql = "INSERT INTO Blob_Tbl(id, binfile, bincontent)";
sql += " VALUES(?, ?, ?)";
ps = con.prepareStatement(sql); ps.setLong(1, id);
ps.setString(2, binFile);
ps.setBlob(3, oracle.sql.BLOB.empty_lob()); ps.executeUpdate();
//DatabaseUtils.closeObject(ps); ps = con.prepareStatement("SELECT bincontent FROM Blob_Tbl WHERE id = " + id + " for update ");
rs = ps.executeQuery(); if (rs.next())
{
oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob(1); /* write blob content */
OutputStream binOut = binContent.getBinaryOutputStream();
BufferedOutputStream out = new BufferedOutputStream(binOut);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(binFile));
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
in.close();
out.close();
}
con.commit();
} catch (Exception e)
{
e.printStackTrace();
try
{
con.rollback();
} catch (SQLException se)
{
}
throw new SQLException(e.getMessage());
} finally
{
DatabaseUtils.closeObject(rs, ps, con);
}
} public static void fetchLob(long id, String filename) throws SQLException
{
Connection con = null;
Statement st = null;
ResultSet rs = null; try
{
con = ConnectionFactory.getConnection(); String sql = "SELECT * From Blob_Tbl Where id = " + id;
st = con.createStatement(); rs = st.executeQuery(sql);
while (rs.next())
{ String binFile = rs.getString("binfile");
oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob("bincontent"); /* read blob content */
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
BufferedInputStream in = new BufferedInputStream(binContent.getBinaryStream()); int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
in.close();
out.close();
} } catch (Exception e)
{
throw new SQLException(e.getMessage());
} finally
{
DatabaseUtils.closeObject(rs, st, con);
}
} public static void main(String[] args) throws Exception
{
if (args.length == 0)
{
addLob(1, "a.jpg");
} else
{
fetchLob(1, args[0]);
}
}
}自己把有的东西改下吧