JAVA中如何处理BLOB类型的数据啊? JAVA中 当要向ORACLE中插入或读取BLOB类型的数据时 如何操作啊! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 完整代码http://blog.csdn.net/caoyinghui1986/archive/2008/04/05/2252772.aspx仅供参考 偶来可耻的蹭点分参考。import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.JFileChooser;/*-- 测试表 create table TestImage( img_id int primary key, img_content blob)*/ public class BlobTestForOracle { private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:oratest"; private static final String DB_USER = "test"; private static final String DB_PASSWORD = "test"; public BlobTestForOracle(){ try { Class.forName(DB_DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 另存为... * * */ public String chooseFileSave(String title,String fileName){ JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(fileName)); chooser.setDialogTitle(title); int rnt = chooser.showSaveDialog(null); if(rnt == JFileChooser.APPROVE_OPTION){ return chooser.getSelectedFile().getAbsolutePath(); }else{ return ""; } } /** * 打开... * * */ public String chooseFileOpen(String title,String fileName){ JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(fileName)); chooser.setDialogTitle(title); int rnt = chooser.showOpenDialog(null); if(rnt == JFileChooser.APPROVE_OPTION){ return chooser.getSelectedFile().getAbsolutePath(); }else{ return ""; } } /** * 写入blob * */ public void writeFileToDatabaseByRid(String fileName,int rid){ BufferedInputStream bis = null; Connection conn = null; PreparedStatement pstmt = null; String strSQL = null; File file = null; int size = 0; try{ strSQL = "INSERT INTO TestImage(img_id,img_content) VALUES(?,?)"; conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); pstmt = conn.prepareStatement(strSQL); fileName = this.chooseFileOpen("请选择一个文件",fileName); file = new File(fileName); if(! file.exists()){ System.out.println(file.getAbsolutePath()); System.out.println("文件貌似不存在!"); return; } else if(file.length() > 50 * 1024 * 1024){ // 限制一下文件大小 System.out.println("文件是不是太大鸟? " + file.length()); return; } size = (int)file.length(); bis = new BufferedInputStream(new FileInputStream(file)); pstmt.setInt(1, rid); pstmt.setBinaryStream(2,bis,size); // 类似于逐一读取,bis流也被耗尽需要reset int effect = pstmt.executeUpdate(); if(effect == 1){ System.out.println("写入ok!" + file.getAbsolutePath()); } else{ System.out.println("写入failed!"); } bis.close(); }catch(IOException ioe){ ioe.printStackTrace(); }catch(SQLException sqle){ sqle.printStackTrace(); }finally{ if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } /** * 从blob读取 * */ public void getFileFromDatabaseByRid(String fileName,int rid){ String strSQL = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rst = null; int size = 0; File file = null; try{ strSQL = "SELECT * FROM TestImage WHERE img_id = ?"; conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); pstmt = conn.prepareStatement(strSQL); pstmt.setInt(1, rid); rst = pstmt.executeQuery(); if(rst.next()){ fileName = this.chooseFileSave("存储为...", fileName); file = new File(fileName); if("".equals(file.getName().trim())){ System.out.println("failed!"); return; } OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); BufferedInputStream bis = new BufferedInputStream(rst.getBinaryStream("img_content")); byte[] b = new byte[10 * 1024]; // 这里是设置了缓存 byte[] b 设置为 10K int len = 0; // 返回read一次实际读取的字节数 while((len = bis.read(b)) != -1){ os.write(b,0,len); // 将b数组的内容写(按照len个字节输出)到输出流中,注意这里不要混淆了b.length size += len; } os.flush(); os.close(); bis.close(); System.out.println("保存文件完毕! " + size + " Byte(s)"); } else{ System.out.println("id : " + rid + " 文件没有找到"); } }catch(IOException ioe){ ioe.printStackTrace(); } catch(SQLException sqle){ sqle.printStackTrace(); }finally{ if(rst != null){ try { rst.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } public static void main(String[] args){ // test BlobTestForOracle test = new BlobTestForOracle(); test.writeFileToDatabaseByRid("C:/a.jpg",123); test.getFileFromDatabaseByRid("C:/a.jpg", 123); }} js问题,求助 网页中的动态图表是怎么实现的? 怎样将图文编辑器的文字改为不可编辑 大伙快来看看,这个JSP页面怎么出错啦!! 中文乱码 求助判断checkbox是否被选中的问题 servlet中怎么能取得站点的绝对路径? 如何同时提交file和其他表单???help!!!!!!! 急!!!请问那里有myEclipse下载? 谁用过水晶报表,近来一下! db2下求最近的一条数据 求一段能实现自动提示信息功能的js代码!谢谢!
http://blog.csdn.net/caoyinghui1986/archive/2008/04/05/2252772.aspx
仅供参考
参考。import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.swing.JFileChooser;/*-- 测试表
create table TestImage(
img_id int primary key,
img_content blob
)*/
public class BlobTestForOracle {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:oratest";
private static final String DB_USER = "test";
private static final String DB_PASSWORD = "test";
public BlobTestForOracle(){
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 另存为...
*
* */
public String chooseFileSave(String title,String fileName){
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(fileName));
chooser.setDialogTitle(title);
int rnt = chooser.showSaveDialog(null);
if(rnt == JFileChooser.APPROVE_OPTION){
return chooser.getSelectedFile().getAbsolutePath();
}else{
return "";
}
}
/**
* 打开...
*
* */
public String chooseFileOpen(String title,String fileName){
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(fileName));
chooser.setDialogTitle(title);
int rnt = chooser.showOpenDialog(null);
if(rnt == JFileChooser.APPROVE_OPTION){
return chooser.getSelectedFile().getAbsolutePath();
}else{
return "";
}
}
/**
* 写入blob
* */
public void writeFileToDatabaseByRid(String fileName,int rid){
BufferedInputStream bis = null;
Connection conn = null;
PreparedStatement pstmt = null;
String strSQL = null;
File file = null;
int size = 0;
try{
strSQL = "INSERT INTO TestImage(img_id,img_content) VALUES(?,?)";
conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
pstmt = conn.prepareStatement(strSQL);
fileName = this.chooseFileOpen("请选择一个文件",fileName);
file = new File(fileName);
if(! file.exists()){
System.out.println(file.getAbsolutePath());
System.out.println("文件貌似不存在!");
return;
}
else if(file.length() > 50 * 1024 * 1024){ // 限制一下文件大小
System.out.println("文件是不是太大鸟? " + file.length());
return;
}
size = (int)file.length();
bis = new BufferedInputStream(new FileInputStream(file));
pstmt.setInt(1, rid);
pstmt.setBinaryStream(2,bis,size); // 类似于逐一读取,bis流也被耗尽需要reset
int effect = pstmt.executeUpdate();
if(effect == 1){
System.out.println("写入ok!" + file.getAbsolutePath());
}
else{
System.out.println("写入failed!");
}
bis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(SQLException sqle){
sqle.printStackTrace();
}finally{
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
/**
* 从blob读取
* */
public void getFileFromDatabaseByRid(String fileName,int rid){
String strSQL = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rst = null;
int size = 0;
File file = null;
try{
strSQL = "SELECT * FROM TestImage WHERE img_id = ?";
conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, rid);
rst = pstmt.executeQuery();
if(rst.next()){
fileName = this.chooseFileSave("存储为...", fileName);
file = new File(fileName);
if("".equals(file.getName().trim())){
System.out.println("failed!");
return;
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
BufferedInputStream bis = new BufferedInputStream(rst.getBinaryStream("img_content"));
byte[] b = new byte[10 * 1024]; // 这里是设置了缓存 byte[] b 设置为 10K
int len = 0; // 返回read一次实际读取的字节数
while((len = bis.read(b)) != -1){
os.write(b,0,len); // 将b数组的内容写(按照len个字节输出)到输出流中,注意这里不要混淆了b.length
size += len;
}
os.flush();
os.close();
bis.close();
System.out.println("保存文件完毕! " + size + " Byte(s)");
}
else{
System.out.println("id : " + rid + " 文件没有找到");
}
}catch(IOException ioe){
ioe.printStackTrace();
}
catch(SQLException sqle){
sqle.printStackTrace();
}finally{
if(rst != null){
try {
rst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args){
// test
BlobTestForOracle test = new BlobTestForOracle();
test.writeFileToDatabaseByRid("C:/a.jpg",123);
test.getFileFromDatabaseByRid("C:/a.jpg", 123); }
}