高分相赠!!!!!!!!!!无法读取blob数据,急!!!!!!!!!!!!!!!!!!!!!!!! 这和你使用的jdbc和数据库是有关系的,应该去参考你用的数据库的jdbc包中blob调用的介绍.另:不一定是没有读出来,有可能没写进去. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上说的有道理,贴上一个既可以插入,又可以从表中读取得程序把自己建张表就可以了(看insert 语句就可以建立了)/** * Created by IntelliJ IDEA. * User: ljt * Date: 2003-3-31 * Time: 18:51:38 * To change this template use Options | File Templates. */import oracle.jdbc.driver.OraclePreparedStatement;import oracle.jdbc.driver.OracleResultSet;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class TestOpenDoc { public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的 public OraclePreparedStatement opst = null; //**PreparedStatement用 public Connection conn = null; public Statement stmt = null; public TestOpenDoc() { } public boolean getConnect() {//这是我的数据库所在 String serverName = "prosrv"; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA"; conn = DriverManager.getConnection(url, "appuser", "appuser"); } catch (Exception e) { System.out.println(e); return false; } return true; } public static void main(String[] args) { TestOpenDoc test = new TestOpenDoc(); if (!test.getConnect()) { System.out.println("数据库连结错误"); return ; } try{ test.conn.setAutoCommit(false); byte a[] = null; //**将测试文件test.doc读入此字节数组 java.io.FileInputStream fin = null; java.io.FileOutputStream fout = null; //Oracle提供的 try { java.io.File f1 = new java.io.File("c:/test.doc"); java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写 //入该文 件,和源文件对比测试用 fin = new java.io.FileInputStream(f1); fout = new java.io.FileOutputStream(f2); int flength = (int) f1.length(); //**读入文件的字节长度 System.out.println("file length::" + flength); a = new byte[flength]; int i = 0; int itotal = 0; //* 将文件读入字节数组 for (; itotal < flength; itotal = i + itotal) { i = fin.read(a, itotal, flength - itotal); } fin.close(); System.out.println("read itotal::" + itotal); //**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化 String mysql = "insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())"; OraclePreparedStatement opst = (OraclePreparedStatement) test.conn. prepareStatement(mysql); opst.setString(1, "wordtemplate2"); opst.setInt(2, flength); opst.executeUpdate(); opst.clearParameters(); // /**插入其它数据后,定位BLOB字段 mysql = "select filebody from filelist where filename=?"; opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql); opst.setString(1, "wordtemplate2"); OracleResultSet ors = (OracleResultSet) opst.executeQuery(); if (ors.next()) { oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段 int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段 System.out.println("j:" + j); test.conn.commit(); ors.close(); } System.out.println("insert into ok"); byte b[] = null; //**保存从BLOB读出的字节 opst.clearParameters(); mysql = "select filebody from filelist where filename=?"; opst = (OraclePreparedStatement) test.conn. prepareStatement(mysql); opst.setString(1, "wordtemplate2"); ors = (OracleResultSet) opst.executeQuery(); if (ors.next()) { oracle.sql.BLOB blob2 = ors.getBLOB(1); System.out.println("blob2 length:" + blob2.length()); b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据 System.out.println("b length::" + b.length); test.conn.commit(); } ors.close(); // 将从BLOB读出的字节写入文件 fout.write(b, 0, b.length); fout.close(); System.out.println("write itotal::" + b.length); } catch (Exception e) { System.out.println("errror :" + e.toString()); e.printStackTrace(); } finally { //**关闭所有数据联接 test.conn.commit(); } } catch(Exception e){ System.out.println(e); }}} http://expert.csdn.net/Expert/topic/1296/1296982.xml?temp=.1974451 java,基础问题,错在哪里了 关于IO输入和输出的问题 getActionCommand() 和getSource()的区别^ 局部类的限制问题? 大家在用table时有没有用到好的扩展包啊? 哪位给看看这个小代码为什么不能运行 notify()和notifyAll()的区别 一个超简单的问题? List l1 = new LinkedList();报错 高手请进:在系统正在运行和不重起的情况下,如何装载指定的 jar 文件 一个非常怪异的问题!!!!!!!!!!!!(UP者有分) java中怎样调用外部dos程序?
自己建张表就可以了(看insert 语句就可以建立了)
/**
* Created by IntelliJ IDEA.
* User: ljt
* Date: 2003-3-31
* Time: 18:51:38
* To change this template use Options | File Templates.
*/
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;public class TestOpenDoc {
public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //**PreparedStatement用
public Connection conn = null;
public Statement stmt = null; public TestOpenDoc() {
} public boolean getConnect() {
//这是我的数据库所在
String serverName = "prosrv";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = DriverManager.getConnection(url, "appuser", "appuser");
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
} public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!test.getConnect()) {
System.out.println("数据库连结错误");
return ;
}
try{ test.conn.setAutoCommit(false);
byte a[] = null; //**将测试文件test.doc读入此字节数组
java.io.FileInputStream fin = null;
java.io.FileOutputStream fout = null; //Oracle提供的
try {
java.io.File f1 = new java.io.File("c:/test.doc");
java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写 //入该文 件,和源文件对比测试用
fin = new java.io.FileInputStream(f1);
fout = new java.io.FileOutputStream(f2); int flength = (int) f1.length(); //**读入文件的字节长度
System.out.println("file length::" + flength);
a = new byte[flength]; int i = 0;
int itotal = 0;
//* 将文件读入字节数组
for (; itotal < flength; itotal = i + itotal) {
i = fin.read(a, itotal, flength - itotal);
}
fin.close(); System.out.println("read itotal::" + itotal);
//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
opst.setInt(2, flength);
opst.executeUpdate();
opst.clearParameters();
// /**插入其它数据后,定位BLOB字段
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) opst.executeQuery(); if (ors.next()) {
oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段
int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段
System.out.println("j:" + j);
test.conn.commit();
ors.close();
} System.out.println("insert into ok"); byte b[] = null; //**保存从BLOB读出的字节
opst.clearParameters();
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
ors = (OracleResultSet) opst.executeQuery();
if (ors.next()) {
oracle.sql.BLOB blob2 = ors.getBLOB(1); System.out.println("blob2 length:" + blob2.length());
b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据
System.out.println("b length::" + b.length);
test.conn.commit();
}
ors.close();
// 将从BLOB读出的字节写入文件
fout.write(b, 0, b.length);
fout.close(); System.out.println("write itotal::" + b.length); }
catch (Exception e) {
System.out.println("errror :" + e.toString());
e.printStackTrace(); }
finally { //**关闭所有数据联接
test.conn.commit();
}
}
catch(Exception e){
System.out.println(e); }
}}